2016-09-12 58 views
1

我結合Cell對象的數組的數據表PrimeNG:Angular2 + 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')); 
     }); 
    } 

所以,我發現了表具有reset()方法清除排序/篩選/選擇狀態。每當URL參數更改並且新數據加載時,我都需要調用它。

但是,我怎樣才能引用dataTable並從ngOnInit()方法中調用reset()方法?

回答

8

你可以充分利用@ViewChild註釋:

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

    // ... 

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

'進口{}數據表從 'primeng /組件/數據表/數據表';之後的' 感謝我。 – Arun

+1

這工作,但它很糟糕,它似乎清除過濾器和其他一切。 – slashp

相關問題