简体   繁体   English

Angular6 动态数组过滤器 (TypeScript)

[英]Angular6 Dynamic Array Filter ( TypeScript )

I have a question about dynamicly filter array.. I making a table and I have different filtering options.我有一个关于动态过滤数组的问题。我制作了一个表格,我有不同的过滤选项。 For example ;例如 ; Only selected column filter, MultiSelect dropdown menu filter and ı want filter multiple columns .仅选择列过滤器,多选下拉菜单过滤器和我想要过滤多个列。 my table我的桌子

For Example ;例如 ; Write a filter word in Brand filter.在品牌过滤器中写一个过滤器词。 This code line is filtered my data.此代码行过滤了我的数据。 (veri = data. (in Turkish) ). (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())); }

I want to filter the filtered data by year but is not working.我想按年份过滤过滤后的数据但不起作用。 And I have a more problem.我还有一个更大的问题。 We were suppose filtered Brand and Year.我们假设过滤了品牌和年份。 if the filter word we wrote is in the array, update my array and display into table.如果我们写的过滤词在数组中,更新我的数组并显示到表中。 OK.好的。 No problem.没问题。 We select colors in dropdown menu.我们在下拉菜单中选择颜色。 After we want filter by colors ( White and Green ).在我们想要按颜色(白色和绿色)过滤之后。 How we make do this?我们如何做到这一点? I don't know how many data will come to filter.我不知道会有多少数据来过滤。 in this point we need to dynamic filter.在这一点上我们需要动态过滤。 Please help me.请帮我。 thank you..谢谢你..

here is my example for filtering by many inputs:这是我通过许多输入进行过滤的示例:

  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();
    }
  }

Variable Filter is array of string.变量过滤器是字符串数组。

x[colName] should work. 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);
    }
  }
}

I created a pipe in which list is filtered and it works.我创建了一个管道,在其中过滤了列表并且可以正常工作。 This is in angular 6这是在角度 6

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

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