【发布时间】:2022-02-01 03:38:05
【问题描述】:
在我的代码中,我有一个搜索栏,我正在尝试根据我编写的内容过滤列表中的元素。如果我按顺序搜索元素,过滤器工作正常。例如,如果我像 'red blue' 一样搜索 'red blue yellow' 但是,当我搜索 'red yellow' 时,我希望过滤器工作。这是我的代码,我应该添加什么来实现我想要的?
HTML:
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
TS:
dataSource: MatTableDataSource<IProduct>;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;
filterPredicate(data: IProduct, filter: string) {
let fullText = "";
fullText += data.StockIntegrationCode;
fullText += data.ProductGroup ? data.ProductGroup.GroupName : "";
fullText += data.ProductCategory
? data.ProductCategory.CategoryName
: "";
fullText += data.ProductName;
fullText += data.Description;
fullText += data.UnitType ? data.UnitType.Name : "";
fullText += data.IsActive ? "Aktif" : "Taslak";
return (fullText as any).toLocaleLowerCase("tr").indexOf(filter) >= 0;
}
applyFilter(filterValue: string) {
if (this.dataSource) {
this.dataSource.filter = filterValue.trim().toLocaleLowerCase();
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
}
【问题讨论】:
-
您能否在 stackblitz.com 上提供您的代码来重现此问题?
-
您分享的代码未显示您遇到问题的部分(搜索/过滤),如果您分享该部分会很有帮助
-
过滤真的没有问题。我只是想通过让用户以无序的方式搜索项目来改进它。
标签: javascript html angular typescript