bircastri bircastri - 6 months ago 29
Javascript Question

How can I get Index from Picker value selected?

I'm building an app with Appcelerator.
So I have used a Picker component to display a list of value.

Now I want to know what is the index of element that the user have selected.

So I'm try to do this:

var indexRow =$.comboDecription.getSelectedRow(0).getZIndex();

but I have undefined value.


You can use the following code:

// first get all columns
var columnsArray = $.comboDecription.getColumns();

// since it is a single column picker, so first index column's rows will be the ones you need.
var allRows = columnsArray[0].rows;   

// get the title of first row, getSelectedRow(index) takes column index which is 0 in this case
var currentRowTitle = $.comboDecription.getSelectedRow(0).title;

// get the titles of all rows, these titles will be used to get the index of current title.
// use underscore library 'map' method to iterate over all rows and get their titles in an array
var allRowsTitles =, function (row) {
    return row.title;

// *** OR *** you can use underscore _.pluck method 
var allRowsTitles = _.pluck(allRows, 'title');

// finally, this is the index of the selected picker row.
var currentSelectedRowIndex = allRowsTitles.indexOf(currentRowTitle);

I understand that it is the long process, but there are other ways too which depends on your implementation process. Nevertheless, I have shown you the stuffs you can do at run-time, and so you can do other things related to pickers.