我想要像material dataTable filter一样,通过一个输入字段使用名字、手机号码过滤我的数据。构造函数:
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;
}
但这是行不通的。有没有人能建议我如何过滤数据。
发布于 2021-09-13 07:54:46
您可以像下面这样实现:
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;
}
https://stackoverflow.com/questions/69159021
复制相似问题