Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add multiple filter values to angular material table? [duplicate]

I am working on angular material table which should be filtered based on selected items from mat-select box. It works fine when I have normal select box where user can select only one item.

But the issue is I have it as multi-select box where user can select more than one filters.

Multiselect stores selected items in an array. How can I pass it to table datasource?

applyFilter() {
   console.log(this.selection); 
   this.dataSource.filter = this.selection.trim().toLowerCase() 
 }

How can I pass array of filter values?

Stackblitz demo

like image 540
IamGrooot Avatar asked Dec 18 '25 09:12

IamGrooot


1 Answers

You can overwrite datasource.filterPredicate with your custom filter logic.

// array containing the selected options
selection: any[];

ngOnInit() {
  this.dataSource.filterPredicate = (userData: UserData, filter: string) => {
    // return true if the userData should be displayed
    return this.selection.length > 0 
      ? this.selection.some(version => version == data.version)
      : true;
  }
}

applyFilter() {
  // we don't use the filter value in filterPredicate and just pass some value here
  // to trigger the filter (there might be better options)
  this.dataSource.filter = 'only used to trigger filter';
}

https://stackblitz.com/edit/angular-7w9ajc-koib2f

like image 104
frido Avatar answered Dec 20 '25 03:12

frido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!