我将一个Cell对象数组绑定到PrimeNG DataTable:
html的:
<p-dataTable [value]="_cells" [responsive]="true" [globalFilter]="gb"> <p-column field="id" header="id" sortable="true"></p-column> <p-column field="name" header="name" sortable="true" ></p-column> </p-dataTable>
.TS:
ngOnInit() { var self = this; // Capture the id in the URL this._route.params.subscribe(params => { self._stationId= params['id']; this._dataService .GetAllCells(self._stationId) .subscribe((data:Cell[]) => this._cells = data,error => alert(error),() => console.log('Retrieved cells')); }); }
所以我发现dataTable有一个reset()方法来清除排序/过滤/选择状态.每当URL参数更改并且正在加载新数据时,我都需要调用它.
解决方法
您可以利用
@ViewChild
注释:
export class MyComponent implements OnInit { @ViewChild(DataTable) dataTableComponent: DataTable; // ... ngOnInit() { this.dataTableComponent.reset(); } }