I've been trying to apply multi column filtering i.e a text input in column headers will filter only on the contents of the column.So far I've been able to make it work by overriding filterPredicate of MatTableDataSource but once I override the default filtering which is across columns no longer works.
export class TableFilteringExample implements OnInit
{
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
positionFilter = new FormControl();
nameFilter = new FormControl();
filteredValues =
{
position: '',
name: '',
weight: '',
symbol: ''
};
ngOnInit()
{
this.positionFilter.valueChanges.subscribe((positionFilterValue) =>
{
this.filteredValues['position'] = positionFilterValue;
this.dataSource.filter = JSON.stringify(this.filteredValues);
});
this.nameFilter.valueChanges.subscribe((nameFilterValue) =>
{
this.filteredValues['name'] = nameFilterValue;
this.dataSource.filter = JSON.stringify(this.filteredValues);
});
this.dataSource.filterPredicate = this.customFilterPredicate();
}
applyFilter(filterValue: string)
{
this.dataSource.filter = filterValue.trim().toLowerCase();
this.dataSource.filter = filterValue;
}
customFilterPredicate()
{
const myFilterPredicate = function(data: PeriodicElement, filter: string): boolean
{
let searchString = JSON.parse(filter);
return data.position.toString().trim().indexOf(searchString.position) !== -1 && data.name.toString().trim().toLowerCase().indexOf(searchString.name)!== -1;
}
return myFilterPredicate;
}
}
What I'm looking for is once column filter is applied the default filter should update the existing filter criteria and return the further filtered data.
