Ionic 2搜索栏按2个属性过滤

问题描述 投票:1回答:2

我有一个数据库读取返回给我一个JSON我在我的*ngFor=""使用,在同一页面我有一个搜索栏搜索用户名,但我也需要它搜索用户的描述(如果它包含我正在打字)。

这是我的代码:

我的搜索栏:

<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,但我一无所获。

如何实现这一目标的最佳方法是什么?有提供者吗?我的回报中有正确的代码行吗?

谢谢 :)

json angular ionic2
2个回答
1
投票

我认为你应该使用OR ||运算符而不是AND &&。 使用&&,您将只看到与名称和描述相匹配的结果。


0
投票

试试这个,

var searchedText = item['name'] + item['surname'];
return (searchedText.toLowerCase().includes(val.toLowerCase()));

定义要搜索的额外字段。

© www.soinside.com 2019 - 2024. All rights reserved.