【问题标题】:Auto suggest dropdown in titanium example?钛示例中的自动建议下拉菜单?
【发布时间】:2013-09-05 06:36:46
【问题描述】:

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

你能推荐一种自动消化的方法吗?

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

【问题讨论】:

    标签: titanium titanium-mobile


    【解决方案1】:

    尝试以下代码并根据您的需要进行修改。这里我使用数组(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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2021-07-10
      相关资源
      最近更新 更多