【问题标题】:When ever i filtered my list using search it gave me filtered value. when my search bar is empty it doesn't gave me my original list back in ionic当我使用搜索过滤我的列表时,它给了我过滤值。当我的搜索栏为空时,它不会在 ionic 中将我的原始列表返回给我
【发布时间】:2020-10-04 12:49:30
【问题描述】:

当我使用搜索过滤我的列表时,它给了我过滤值。当我的搜索栏为空时,它不会在 ionic 中返回我的原始列表。

page.html

<ion-searchbar 
  [(ngModel)]="searchTerm"  
  [(ngModel)]="searchList"
  (input)="setFilteredTitle()" 
  class="search">
</ion-searchbar>

Page.ts

filterItems(searchTerm){
  return this.result.filter(item => {
    return item.category_name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
  });
}

filterList(searchList){
  return this.result2.filter(items => {
    return items.store_name.toLowerCase().indexOf(searchList.toLowerCase()) > -1;
  });
}

setFilteredTitle(){
  this.showlist = true
  this.result = this.filterItems(this.searchTerm)
  this.result2 = this.filterList(this.searchList)
}

在 ionic 中,我想从一个搜索栏中搜索两个不同的列表,我使用了这些功能。它过滤了我的数据。但是,每当我从搜索栏中删除值时,它都不会返回我的原始列表,并且在刷新之前不会再次过滤。有什么问题请提前帮助thanx。

【问题讨论】:

  • 您确定在一个输入字段中有多个ngModel
  • 是的,我知道这可能是错误的,但它对我有用.. 你还有其他解决方案吗,请建议thanx
  • 发生的事情是您将搜索结果绑定到包含列表的同一数组中,因此当您搜索例如匹配 2 并显示它们时,现在主数据列表替换为这2个结果,当清除搜索框字符时,将找到0个结果,因此将清除主数据数组,因此您可以更改搜索方式或添加另一个数组以显示找到的结果并将ngIf隐藏起来主数组,如果 searchBox 正在搜索,但优先使用搜索管道进行搜索。

标签: angular ionic-framework search filter ionic4


【解决方案1】:

使用您当前的逻辑,您只能在数组中匹配的情况下获取值,并且当您清除该值时它不匹配任何内容并且您得到一个空的Array。因此,如果您在 searchTerm 中没有得到任何内容,则需要更改逻辑以返回相同的数组。您的代码应该看起来像这样。我没有玩过ion-searchbar,所以我不知道这里的列表变量,但你明白我在说什么。

filterItems(searchTerm){
  if(!searchTerm) return this.result;
  return this.result.filter(item => {
    return item.category_name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
  });
}

filterList(searchList){
  if(!searchList) return this.result2;
  return this.result2.filter(items => {
    return items.store_name.toLowerCase().indexOf(searchList.toLowerCase()) > -1;
  });
}

【讨论】:

  • 好的,我会检查的
  • 好的。所以我们正在更新我们从中取出值的同一个变量。所以我们过滤 resultresult2 并将它们更新为新值。要从中过滤值,您需要有一个不会发生变异的全局变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
相关资源
最近更新 更多