2016-01-15 72 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