我如何从选择器值获得索引选定

How can I get Index from Picker value selected?

本文关键字:索引 选择器      更新时间:2023-09-26

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

现在我想知道用户选择的元素的索引是什么

所以我试着这样做:

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

您可以使用以下代码:

// 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);

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

gZindex()返回视图所在的图层。change事件返回当前选择的索引。