Angular2 + PrimeNG - 当底层数据发生变化时如何重置数据表?

2024-03-08

我正在绑定一个数组CellPrimeNG 数据表的对象:

.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 参数发生变化并且加载新数据时,我都需要调用它。

但是我如何引用 dataTable 并调用reset()方法从内部ngOnInit() method?


您可以利用@ViewChild https://angular.io/docs/ts/latest/api/core/index/ViewChild-var.html注解:

export class MyComponent implements OnInit {
    @ViewChild(DataTable) dataTableComponent: DataTable;

    // ...

    ngOnInit() {
        this.dataTableComponent.reset();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular2 + PrimeNG - 当底层数据发生变化时如何重置数据表? 的相关文章

随机推荐