I want to filter my data using First name , mobile number by one input field like material dataTable filter. constructor:
constructor() {
this.filteredPatient = this.patientsearch.valueChanges
.pipe(
startWith(''),
map(p => p ? this._filterPatient(p) : this.patients.slice())
);
}
private _filterPatient(value: string): IPatient[] {
const filterValue = value.toLowerCase();
const searchByfirstName = this.patients.filter(p => p.firstName.toLowerCase().includes(filterValue));
const searchBylastName = this.patients.filter(p => p.lastName.toLowerCase().includes(filterValue));
const searchBymobileNumber = this.patients.filter(p => p.mobileNumber.toLowerCase().includes(filterValue));
const mergedObj = { ...searchByfirstName, ...searchBymobileNumber };
return mergedObj;
}
but this is not working . Can anyone suggest me how can I filter data .
You can achieve that like the following:
private _filterPatient(value: string): IPatient[] {
const filterValue = value.toLowerCase();
const result = this.patients.filter(
(p) =>
p.firstName?.toLowerCase().includes(filterValue) ||
p.lastName?.toLowerCase().includes(filterValue) ||
p.mobileNumber?.toLowerCase().includes(filterValue)
);
return result;
}