【问题标题】:Ionic 2 searchbar filter by 2 propertiesIonic 2 搜索栏按 2 个属性过滤
【发布时间】: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,
  }, ...
}

到目前为止,我唯一尝试的是在我的回报中添加一个&amp;&amp; item.description.toLowerCase().indexOf(val.toLowerCase()) &gt; -1,但我什么也没得到。

实现这一目标的最佳方法是什么?与供应商?我返回的代码行正确吗?

谢谢:)

【问题讨论】:

    标签: json angular ionic2


    【解决方案1】:

    我认为您应该使用 OR || 运算符而不是 AND &amp;&amp;
    使用&amp;&amp;,您只会看到同时匹配namedescription 的结果。

    【讨论】:

      【解决方案2】:

      试试这个,

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

      定义要搜索的额外字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-03
        • 2017-11-14
        • 2017-09-01
        • 2017-06-29
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多