首页 > 解决方案 > dataSource.filter 过滤在前端不可见的 ID

问题描述

基本上,这是我的代码

  applySearchFilter(filterValue: string) {
this.values = filterValue.toString().toLowerCase();
this.dataSource.filter = this.values;

} 使用“this.dataSource.filter”我可以从中过滤掉任何数据,但我想为 keyId 添加一个例外......请参见下面的对象。

我的对象看起来像这样。

{
keyId: "00000000-0000-0000-0000-000000010001", 
name: "Test 10",
location: "New York"
}

当我输入“10”时,它会过滤数据源中包含 10 的所有内容。因此,不仅名称中包含“10”,而且 keyId 中也包含。我花了一个小时才意识到这一直在发生……这是因为 keyId xD

尽管我得到了包含 keyId 的整个对象,但 keyId 没有显示在前端,并且不应该是搜索/过滤的。即使它没有在前端显示 keyId,他也会搜索具有该 keyid 的对象

我现在不知道如何修复它,因为像this.dataSource.data.name这样的东西不起作用......

如果你们女孩和女孩能给我一些建议,我将不胜感激。

哦,这是我将数据过滤到的地方。

  public getObject(): void {
   this.service.getObjectMethod().then(data=> {
   this.dataSource.data = data;
}, (err: Error) => { console.log(err.message); });

}

标签: angularjsangularfilter

解决方案


我解决了这个问题。

public filterData(): void {
    if (this.inputValues.trim().length !== 0) {
      this.filteredValues = [];
      this.dataSource.data.filter(data => {
        if (data.name.toLowerCase().includes((this.inputValues).toLowerCase()) ||
          data.opensCategory.toLowerCase().includes((this.inputValues).toLowerCase())) {
          return true && this.filteredValues.push(data);
        }
      });
      this.dataSource.data = this.filteredValues;
    } 
  }

推荐阅读