0

I'm trying to display object value in drop down, and want to get the entire object value when i select any of the value. How do I set that?

I'm trying to access the Object value to display by using "." operator like below

<dd:DropDown items="{{ raw_material_list.productName }}" selectedIndex="{{ raw_material_index }}" opened="dropDownOpened" closed="dropDownClosed" selectedIndexChanged="dropDownSelectedIndexChanged" class="dropDownStyle" />

Data which I'm passing is like below

raw_material_list = [
     {
       "id": "44",
       "created_date": "2019-04-19 12:01:13",
       "activeFlag": "true",
       "productType": "purchase",
       "productName": "suraksha",
     },
     {
       "id": "43",
       "created_date": "2019-04-19 11:59:59",
       "activeFlag": "true",
       "productType": "purchase",
       "productName": "vajra",
     }
   ];

I need to get the result exactly like i mentioned, any help is welcome.

2 Answers2

1

I have updated your playground here

Changed the display to productName in drop-down-common.js

ValueList.prototype.getDisplay = function (index) {
        if (types.isNullOrUndefined(index)) {
            return null;
        }
        if (index < 0 || index >= this.length) {
            return "";
        }
        return this._array[index].productName;
    };

P.S. You must use ValueList for the Object.

Narendra
  • 4,514
  • 2
  • 21
  • 38
  • i'm not getting why it is displaying items. instead of the data which i gave. the solution u gave makes me more confused. you have changed display to this._array[index].productName , and the binding in dropdown you changed it from raw_material_list to items. but i'm not getting where this items is coming from. – vikas acharya May 10 '19 at 06:16
  • for (var loop = 0; loop < 200; loop++) { items.push({ value: "I" + loop, productName: "Item " + loop }); // Here iam trying to extract the value. } – Narendra May 10 '19 at 06:17
  • I have added them in home-page.js – Narendra May 10 '19 at 06:17
-1

the above answer was right, but for the question the very easy and straight forward solution is

var nativescript_drop_down_1 = require("../nativescript-drop-down") //Plugin

1> modify the array list like below

raw_material_list = new nativescript_drop_down_1.ValueList([
     {
       "id": "44",
       "created_date": "2019-04-19 12:01:13",
       "activeFlag": "true",
       "productType": "purchase",
       "productName": "suraksha",
     },
     {
       "id": "43",
       "created_date": "2019-04-19 11:59:59",
       "activeFlag": "true",
       "productType": "purchase",
       "productName": "vajra",
     }
   ]);

2>to access data 

let obj = viewModel.get("raw_material_list")._array[viewModel.get("raw_material_index")];

      console.log("----obj-----");
      console.log(obj);
Vikas Acharya
  • 3,550
  • 4
  • 19
  • 52
  • Your answer is the extension of my answer only. You can't just convert your array to ValueList and get the desired untill you change the local copy of show `display` to `productName`. – Narendra May 13 '19 at 03:59