如何在 primeng 中使用默认排序重置 p-table?

How to make p-table reset with default sorting in primeng?

我正在从 API 加载数据。数据量很大所以我用的是懒加载

在页面加载时,我通过在 HTML 模板中提供输入来对 table 进行排序,如下所示,它工作正常

<p-table [value]="data" [rows]="10" sortField="id" [sortOrder]="-1" 
  [lazy]="true" (onLazyLoad)="loadLazy($event)">

但我的页面也有重置按钮,因此如果用户单击该按钮,我会尝试将其重置为默认值。

所以在我的组件代码中我使用了下面的代码

this.dataTable.reset();

问题是我正在尝试对 table 进行排序。所以我使用了下面的代码,

this.dataTable.reset();
this.dataTable.sortField = 'id';
this.dataTable.sortOrder = 1;

使用上面的代码 table 得到排序,问题是 API 调用了 3 次。所以我试图在 table 重置时实现排序。所以我尝试了下面的代码,

this.dataTable.reset({'sortField':'id',sortOrder: 1});

但是我收到了错误。我该怎么做?

问题是 sortField 和 sortOrder 是 setter 和触发器加载。 例如,您可以通过这种方式破解它。

this.dataTable.lazy = false;
this.dataTable.reset();
this.dataTable._sortOrder = 1;
this.dataTable.lazy = true;
// calls the endpoint
this.dataTable.sortField = 'id';

但是您的 onLazyLoad 方法当然可以检查是否设置了 $event.sortField 和 return.

我会选择第二个选项,因为这不是黑客攻击,只是验证。关键是必须像您的示例一样最后设置 sortField。

我尝试了@tano 提供的答案,但没有维护默认排序 ('id')。

this.dataTable.lazy = false;
this.dataTable.reset();
this.dataTable._sortOrder = 1;
this.dataTable._sortField = 'id';
this.dataTable.lazy = true;
// calls the endpoint
this.dataTable.sortSingle();