如何从选择的 Picker 值中获取索引?

How can I get Index from Picker value selected?

我正在使用 Appcelerator 构建应用程序。 所以我使用了一个 Picker 组件来显示一个值列表。

现在我想知道用户选择的元素索引是多少。

所以我尝试这样做:

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

但我有未定义的值。

gZindex() returns 视图所在的图层。 更改事件return当前选定的索引。

您可以使用以下代码:

// 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 = _.map(allRows, 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);

我知道这是一个漫长的过程,但是还有其他方法,这取决于您的实施过程。尽管如此,我已经向您展示了您在 运行 时间可以做的事情,因此您可以做其他与选择器相关的事情。