1

我能够在 jquery 中实现自动建议下拉。现在我在钛中实现相同的功能,但是当我用谷歌搜索时,我在钛中的自动建议下拉中没有发现太多。

你能建议一种自我消化的方式吗?

意味着我有一个数组(元素)。当我单击文本字段时,它会显示相关元素。

4

1 回答 1

0

尝试以下代码并根据您的需要进行修改。在这里,我使用数组(searchArray)作为数据存储(您可以根据需要将其替换为数据库字段或源)。

//Table view showing your autocomplete values
var tblvAutoComplete = Ti.UI.createTableView({
    width           : '100%',
    backgroundColor : '#EFEFEF',
    height          : 0,
    maxRowHeight    : 35,
    minRowHeight    : 35,
    allowSelection  : true
});
//Starts auto complete
txtAutoComplete.addEventListener('change', function(e){ 
    var pattern = e.source.value;
    var tempArray = PatternMatch(searchArray, pattern);
    CreateAutoCompleteList(tempArray);
});
//You got the required value and you clicks the word
tblvAutoComplete.addEventListener('click', function(e){
    txtAutoComplete.value = e.rowData.result; 
});

//Returns the array which contains a match with the pattern
function PatternMatch(arrayToSearch, pattern){
    var searchLen = pattern.length;
    arrayToSearch.sort();
    var tempArray = [];
    for(var index = 0, len = arrayToSearch.length; index< len; index++){
        if(arrayToSearch[index].substring(0,searchLen).toUpperCase() === pattern.toUpperCase()){
            tempArray.push(arrayToSearch[index]);
        }
    }
    return tempArray;
}
//setting the tableview values
function CreateAutoCompleteList(searchResults){
    var tableData = [];
    for(var index=0, len = searchResults.length; index < len; index++){

            var lblSearchResult = Ti.UI.createLabel({
                top            : 2,
                width          : '40%',
                height         : 34,
                left           : '5%',
                font           : { fontSize : 14 },
                color          : '#000000',
                text           : searchResults[index]
            });

            //Creating the table view row
            var row = Ti.UI.createTableViewRow({
               backgroundColor : 'transparent',
               focusable       : true,
               height          : 50,
               result          : searchResults[index]
            });

            row.add(lblSearchResult);
            tableData.push(row);
    }
    tblvAutoComplete.setData(tableData);
    tblvAutoComplete.height = tableData.length * 35;
}

这段代码在 ios 和 android 中都适用于我。希望你的问题得到解决:D

于 2013-09-05T09:28:22.147 回答