问题似乎出在服务器端,你正在获取一个特定的结果页面,然后进行过滤,因此你得到了这种行为。为了解决这个问题,您应该首先在服务器上过滤搜索字符串,然后按页面获取数据。
我建议您放弃角度侧过滤,而专注于过滤API侧的数据。
...
export class OpdPatientsComponent implements OnInit {
public opdPatients: Opd[] = [];
public filteredOpdPatients: Opd[] = [];
public paginationData: Pagination = {} as Pagination;
public componentQuery = new HttpParams();
public searchQuery: string = '';
constructor(
private coreService: CoreService,
private router: Router,
private dashboardService: DashboardService,
private ngbModal: NgbModal
) {}
ngOnInit(): void {
this.getPatients();
}
getPatients(): void {
this.componentQuery.set('searchString', this.searchQuery);
this.coreService.getOpds(this.componentQuery).subscribe({
next: (res: any) => {
this.opdPatients = res.data;
this.paginationData = res;
this.filterPatients();
}
});
}
paginateTo(page: number): void {
this.componentQuery = this.componentQuery.set('page', page);
this.getPatients();
}
}
我们还可以去掉TS和HTML中的过滤代码:
<div>
<input type="text" [(ngModel)]="searchQuery" />
</div>
...