How To Add Multiple Filters In Angular Mat Table?
HTML this is the code of my component.html file in which i am adding filters on a table:
Solution 1:
I think that your custom filter has some errors. You need take account when searchString.employee and searchString.project are null one or both
See that, in your code, if the employeeFilter is null or empty employeeFound is true and if projectFilter is null or empty projectFound is also true
Here is the response, but To complementary I make a stackblitz based in the tipical example of material Angular with the periodic elements.
To improve and simplify, I put all the FormControls in a FormGroup
form = new FormGroup({
name: new FormControl(" '<="" span="">),
position: new FormControl(),
and: new FormControl(false)
});
To simply subscribe to valueChanges
this.form.valueChanges.subscribe(res => {
this.dataSource.filter = JSON.stringify(res);
});
my customFilter is like
customFilter = (data: PeriodicElement, filter: string) => {
const filterData = JSON.parse(filter);
//see that if filterData.name=null nameOk=true
const nameOk =
data.name.toLowerCase().indexOf(filterData.name.toLowerCase()) >= 0;
//see that if filterData.position=null positionOk=true
const positionOk = !filterData.position || data.position == filterData.position;
if (filterData.and) { //there're no problemo
return nameOk && positionOk;
} else { //take account when filterData.name or filterData.position is null
if (filterData.name && filterData.position) return nameOk || positionOk;
else {
if (filterData.name) return nameOk;
if (filterData.position) return positionOk;
return true;
}
}
};
I hope sirve you as inspiration
Post a Comment for "How To Add Multiple Filters In Angular Mat Table?"