Angular2 PrimeNG – 当底层数据发生变化时如何重置dataTable?

前端之家收集整理的这篇文章主要介绍了Angular2 PrimeNG – 当底层数据发生变化时如何重置dataTable?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我将一个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参数更改并且正在加载新数据时,我都需要调用它.

但是如何引用dataTable并从ngOnInit()方法调用reset()方法

解决方法

您可以利用 @ViewChild注释:

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

    // ...

    ngOnInit() {
        this.dataTableComponent.reset();
    }
}

猜你在找的Angularjs相关文章