【问题标题】:Angular Unordered Sentence SearchAngular 无序句子搜索
【发布时间】: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


【解决方案1】:

想象一下你可以使用

fullText=fullText.loLocaleLowerCase("tr")
return filter.split(' ').filter(x=>(fullText.indexOf(x)<0)).length==0

那,拆分你的“过滤器”,然后你就有了,例如["red","yellow"] 并仅过滤数组中不在全文中的元素。

如果没有 (length==0) 是因为所有内容都在全文中。

注意:我编写 fullText=fullText.loLocaleLowerCase("tr") 是为了不对过滤器中的每个“单词”重复。

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多