Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material mat-table filter mat-table column containing boolean

I am trying to filter an Angular Material mat-table for a column that only contains boolean. I read here that datasource.filterPredicate can be used to filter the collection but I found out that the call uses a string parameter. Here is my code so far

this.dataSource.filterPredicate = (data: T, filter: boolean) => !filter || data.isCompleted== filter;

I also tried the following but nothing happens

this.dataSource.filteredData.filter(f => f.isCompleted== filter);

How can I filter the datasource based on a boolean column?

Thanks

like image 455
c0micrage Avatar asked Jan 16 '26 23:01

c0micrage


1 Answers

After playing around with mat-table, filterPredicate, I got it work. Here is the fix

I can't compare to a boolean value. To get this to work, I pass in a string of 'true' or 'false'

TS
  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

HTML

applyFilter('true')
applyFilter('false')
like image 62
c0micrage Avatar answered Jan 19 '26 19:01

c0micrage



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!