2016-01-15 75 views
0

我是一位JavaScript开发人员,需要一些帮助来搜索和显示数据。Appcelerator Javascript搜索功能

我有一个TableView(在index.xml中),它从index.js获取数据,并且数据在屏幕上显示正常。我也创建了一个文本框和搜索按钮,但我需要帮助提取用户输入并搜索存储的数据。

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() { 



}; 

我希望用户在SearchtextField键入 '锤',然后按搜索按钮,开始查找功能和显示'锤子'。

谢谢你

回答

1

所以,而不是回答你问的问题,我会提供一种不同的方法。

在手机上,用户需要一个一致的用户界面,这意味着列表上方的“搜索框”或“搜索栏”(您的案例中的桌面视图)。值得庆幸的是,Appcelerator tableview对象包含一个'搜索'内建,您不必编码。点击这里,查看文档:(http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.TableView-property-search

也有TableViews莅临指导(http://docs.appcelerator.com/platform/latest/#!/guide/TableViews-section-29004930_TableViews-Searchingwithinatable

搜索将自动找到标题在搜索字段中输入的值匹配的任何行中的例子。所以只需要输入'hammer',它会将行过滤为一个。

Ray

+0

谢谢你。它允许我用一行代码搜索 但是,如果你能为我提供的原始问题的答案可以帮助我很多在将来 – Wahm

+0

所以,你可以访问一个tableview行(row.title)的内容,所以,如果你想使用你的方法,那么你将需要遍历tableview行,搜索row.title匹配您的搜索字段中的值。 – Ray