Search code examples
titaniumtitanium-mobile

Auto suggest dropdown in titanium example?


I am able to implement auto suggest drop down in jquery .Now I am implementing a same functionality in titanium but when i googled I don't found much in autosuggestion drop down in titanium .

can you suggest a way of autogestion.

Mean I have a array (element).When I click on textfield it show related element.


Solution

  • Try the following code and modify as per your need. Here I Used array(searchArray) as data storage(You can replace it with database field or source whatever as per your requirement).

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

    This code worked for me in both ios and android. Hope your problems get resolved:D