Live Search on Titanium
I'm doing a live search where I execute queries as the user types.
I have a listener on my search field, and every time the user hits a key on the keyboard, I do a new search query.
searchField.addEventListener('change',function(e){
var rows = db.execute('...'); //Query
var data=[];
//Loop through each row
while (rows.isValidRow()) {
//Create row
var row = Titanium.UI.createTableViewRow({
className: 'searchResultsRow'
});
//Create row labels
var rowTitle = Titanium.UI.createLabel({
text:rows.fieldByName('LocationID'),
//text style
});
var rowSubTitle = Titanium.UI.createLabel({
text:rows.fieldByName('Name'),
//text style
});
row.add(rowTitle);
row.add(rowSubtitle);
row.className = 'searchResultsRow';
data.push(row);
searchResults.setData(data);
rows.next();
}
rows.close();
});
This is all fine and good, except that the search is pretty slow. I know this is because if the user types 4 letters really quickly, my app does a query on 1 letter, another query on 2 letters, etc. until finally doing that last query.
How do I cut a prior query short and always be querying on the latest text change?
2 Answers
-
possible solution ( just brainstorming here ):
maybe on change, detect how many characters are in the searchField ( the length of the text inside of it ). then create a setTimeout 1/2 second later or however many milliseconds later, and then in that setTimeout compare what the length of the characters are now compared to what they were BEFORE the setTimeout. if they are the same, then run your query. if they aren't, don't. that can help compensate for someone typing fast.
rocksteady,
danno~ -
Hi Clifton, were you able to get this live search to work?
I am trying to do the same thing, however, I am newbie, not sure how I can query against a sqlite database. Do you have a working sample code you can share?
thanks in advance.