Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nativescript Use LIstPicker with JS object

I'm trying to use ListPicker with an object array and my list gets rendered with the label displaying [object Object] for all elements.

I would like to specify which property to use as the "label" for the listpicker.

Unfortunately Nativescript ListPicker only accepts an array of strings and I can't use my Object array as the label will call toString()

I found an alternative solution based on: https://github.com/NativeScript/NativeScript/issues/1677

buy my app uses page-router-outlet and doesn't use element so I have no way of using the proposed approach above. Given this scenario are there any possible ways of using ListPicker with object arrays or any workaround that doesn't rely on Page element loaded event ?

like image 879
guilhebl Avatar asked Dec 14 '22 23:12

guilhebl


1 Answers

There's an undocumented pull request that makes binding to objects much easier: https://github.com/NativeScript/NativeScript/pull/6033

Adds new properties to the ListView component (textField and valueField - should be used with arrays of JSON objects):

textField - tells the listview which property should be used to display each item.

valueField - tells the listview, which property should be used to update the selectedValue.

selectedValue - is the property that will contain the selectedValue, if valueField is specified, then it will contain the value from that property, otherwise it will contain the whole selected item

Example (Angular):

<ListPicker [items]="printers" textField="name" valueField="id"></ListPicker>

In class:

const printers = [{name: "Printer 1", id: 1}, {name: "Printer 2", id: 2}];
like image 167
DarkNeuron Avatar answered Dec 27 '22 10:12

DarkNeuron