Angular2 + PrimeNG-当基础数据更改时如何重置dataTable? [英] Angular2 + PrimeNG - How to reset dataTable when the underlying data changes?

查看:227
本文介绍了Angular2 + PrimeNG-当基础数据更改时如何重置dataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Cell对象的数组绑定到PrimeNG DataTable:

I'm binding an array of Cell objects to a 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参数更改并且正在加载新数据时,我都需要调用它.

So I found out the dataTable has a reset() method to clear the sorting/filtering/selection state. I need to call it whenever the URL parameter changes and new data is being load.

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

But how can I reference the dataTable and call the reset() method from inside the ngOnInit() method?

推荐答案

您可以利用

You could leverage the @ViewChild annotation:

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

    // ...

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

这篇关于Angular2 + PrimeNG-当基础数据更改时如何重置dataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆