【发布时间】: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