【发布时间】:2017-06-25 22:56:30
【问题描述】:
我有一个数据库读取返回我在我的*ngFor="" 中使用的 JSON,在同一页面中我有一个按用户名搜索的搜索栏,但我还需要它来搜索用户的描述(如果它包含我正在输入的单词)。
这是我的代码:
我的搜索栏:
<ion-searchbar placeholder="Search..." (ionInput)="searchUser($event)"></ion-searchbar>
我的 searchUser 方法(与 ionic 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,但我什么也没得到。
实现这一目标的最佳方法是什么?与供应商?我返回的代码行正确吗?
谢谢:)
【问题讨论】: