gpt4 book ai didi

javascript - 如何以 Angular 过滤表格数据

转载 作者:行者123 更新时间:2023-11-30 09:10:44 25 4
gpt4 key购买 nike

export class EmployeeDetailsComponent implements OnInit {
employees: any = [];
public errorMsg;
// you can define also below
// errorMsg:any = [];
constructor(private _employeeService: EmployeeService) {}

ngOnInit() {
this._employeeService.getEpmloyees().subscribe(
data => (this.employees = data),
error => (this.errorMsg = error)
);
}

applyFilter(filterValue: string) {
this.employees.filter = filterValue.trim().toLocaleLowerCase();
}
}
<body>
<p><input type="text" (keyup)="applyFilter($event.value)"></p>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
<th>Mobile</th>
</tr>

<tbody *ngFor="let employee of employees; i as index">
<tr>
<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.age}}</td>
<td>{{employee.mob}}</td>
</tr>
</tbody>
</table>
</body>

上面是我的代码我只是想知道哪里出了问题在过滤器方法中,我确实需要更改才能使它正常工作。以及如何获取正确过滤的数据?

最佳答案

试试这个

export class EmployeeDetailsComponent implements OnInit {
employees: any = [];
allEmployees:any=[];
public errorMsg;
constructor(private _employeeService: EmployeeService) {}

ngOnInit() {
this._employeeService.getEpmloyees().subscribe(
data => (
this.employees = data,
this.allEmployees=data;//You don't want to override you search data with all employee list
),
error => (this.errorMsg = error)
);
}

applyFilter(filterValue: string) {
let filterValueLower = filterValue.toLowerCase();
if(filterValue === '' ) {
this.employees=this.allEmployees;
}
else {
this.employees = this.allEmployees.filter((employee) => employee.name.includes(filterValueLower)
}
}
}

关于javascript - 如何以 Angular 过滤表格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com