Reputation: 13
I am a new developer to Javascript and need some help searching and displaying data.
I have a TableView (in index.xml) which is getting data from index.js and the data is showing on the screen fine. I have also created a textfield and search button but I need help with extracting the users input and searching the stored data.
index.xml -
<Button id="searchButton" onClick="find" class="button">Search</Button>
<TextField id="SearchtextField" hintText="Search for products: "/>
index.js -
var myproducts = Alloy.Collections.products;
var product = Alloy.createModel('products', {title: 'toy' , desc: 'Toys'});
var product1 = Alloy.createModel('products', {title: 'Yo-yo' , desc: 'Toys'});
var product2 = Alloy.createModel('products', {title: 'Hammer' , desc: 'Hardware'});
myproducts.add(product);
myproducts.add(product1);
myproducts.add(product2);
product.save();
product1.save();
product2.save();
function find() {
};
I want the user to type 'Hammer' in the SearchtextField then press the search button which initiates the find function and the display 'Hammer'.
Thank You
Upvotes: 0
Views: 140
Reputation: 486
So, instead of answering your question as asked, I'm going to provide a different approach.
On mobile, users expect a consistent user interface and that means a 'searchbox' or 'searchbar' above a list (tableview in your case). Thankfully, the Appcelerator tableview object includes a 'search' builtin that you don't have to code. Check out the documentation here: (http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.TableView-property-search)
There is also an example in the TableViews guide here (http://docs.appcelerator.com/platform/latest/#!/guide/TableViews-section-29004930_TableViews-Searchingwithinatable)
The search will automatically find any row where the title matches the values entered in the search field. So just start typing 'hammer' and it will filter down the rows to just one.
Ray
Upvotes: 1