2017-02-08 50 views
1

我有一個數據庫讀取,返回給我一個JSON我用我的*ngFor="",在同一頁我有一個搜索欄,用戶名搜索,但我也需要它o按用戶的描述搜索(如果它包含我輸入的單詞)。Ionic 2搜索欄過濾器由2個屬性

這裏是我的代碼:

我的搜索欄:

<ion-searchbar placeholder="Search..." (ionInput)="searchUser($event)"></ion-searchbar> 

我searchUser方法(同一種離子2個文檔給你):

searchUser(ev: any) { 
    // Reset items back to all of the items 
    this.initializeItems(); 

    // set val to the value of the searchbar 
    let val = ev.target.value; 

    // if the value is an empty string don't filter the items 
    if (val && val.trim() != '') { 
    this.items = this.items.filter((item) => { 
     return (item.name.toLowerCase().indexOf(val.toLowerCase()) > -1); 
    }) 
    } 
} 

我的JSON

的例子
{ 
    "key": { 
    "name": name, 
    "description": desc, 
    "city": city, 
    "state": state, 
    }, ... 
} 

我到目前爲止嘗試過的唯一一件事是爲我的回報添加一個&& item.description.toLowerCase().indexOf(val.toLowerCase()) > -1,但我什麼也沒得到。

實現此目標的最佳方法是什麼?有了供應商?我的回報中有正確的代碼行嗎?

謝謝:)

回答

1

我認爲你應該使用的||操盤手和&& OR。
With &&您只會看到與名稱說明相匹配的結果。