繁体   English   中英

Angular6 动态数组过滤器 (TypeScript)

[英]Angular6 Dynamic Array Filter ( TypeScript )

我有一个关于动态过滤数组的问题。我制作了一个表格,我有不同的过滤选项。 例如 ; 仅选择列过滤器,多选下拉菜单过滤器和我想要过滤多个列。 我的桌子

例如 ; 在品牌过滤器中写一个过滤器词。 此代码行过滤了我的数据。 (veri = 数据。(土耳其语))。

    public filter(str, i) {

      const collName = this.bizimKolon[i].Baslik; // Column Name
      this.veri = this.yedekVeri.filter(x => x[collName].toString().toLowerCase().includes(str.toLowerCase())); }

我想按年份过滤过滤后的数据但不起作用。 我还有一个更大的问题。 我们假设过滤了品牌和年份。 如果我们写的过滤词在数组中,更新我的数组并显示到表中。 好的。 没问题。 我们在下拉菜单中选择颜色。 在我们想要按颜色(白色和绿色)过滤之后。 我们如何做到这一点? 我不知道会有多少数据来过滤。 在这一点上我们需要动态过滤。 请帮我。 谢谢你..

这是我通过许多输入进行过滤的示例:

  applyFilter() {
    let custs = this.ch.customers.filter((cust: Customer) => {
      return  (
          ((cust.name.toLowerCase().trim().indexOf(this.filters[0]) !== -1) || (!this.filters[0])) &&
          ((!this.filters[5]) || (cust.user.username.toLowerCase().trim().indexOf(this.filters[5]) !== -1)) &&
          ((!this.filters[2]) || (cust.state.name.toLowerCase().trim().indexOf(this.filters[2]) !== -1)) &&
          ((!this.filters[1]) || (cust.description.toLowerCase().trim().indexOf(this.filters[1]) !== -1)) &&
          ((!this.filters[3]) || (cust.last_mess.toLowerCase().trim().indexOf(this.filters[3]) !== -1))
      );
    });

    this.dataSource.data = custs;
    this.ch.customers_filter = this.dataSource.data;
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

变量过滤器是字符串数组。

x[colName] 应该可以工作。

@Pipe({name: 'dataListFilterPipe'})
export class DataListFilterPipe implements PipeTransform {
  transform(data: any, col: string, value: string): any {
    if (data !== undefined) {
      return data.filter(x => x[col] == value);
    }
  }
}

我创建了一个管道,在其中过滤了列表并且可以正常工作。 这是在角度 6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM