首页 > 解决方案 > Angular Datatable-在角度 5 中第二次更新表时没有刷新

问题描述

我正在使用角度数据表https://l-lin.github.io/angular-datatables/#/welcome

在页面加载数据表上调用 API 后工作正常。但是当我第二次更新表时,我遇到了一些问题,例如:

1) 数据显示但显示条目未更新,如 Showing 1 to 5 of 5 entries.It 应该是“显示 1 到 1 的 1 条目”第二次而不是“显示 1 到 5 的 5 条目”

2)当我搜索特定记录时,它是以前数据中的搜索记录,而不是当前数据。

所以我尝试了一些解决方案,比如

第二次更新数据时,我销毁第一个数据表实例,然后再次渲染数据表。为此,我编写了类似的函数

rerender(): void {
 THAT.datatableElement.dtInstance.then((dtInstance: DataTables.Api) => {
  // Destroy the table first
  dtInstance.destroy();
  // Call the dtTrigger to rerender again
  THAT.dtTrigger.next();
 });
}

标签: angularangular-datatables

解决方案


使用以下方法重新绑定数据表,即 this.rebindDataTable();

rebindDataTable() {
        this.datatableElement.dtInstance.then(x => x.draw());
}

推荐阅读