清除ag-grid和angular2

时间:2018-02-06 19:24:03

标签: angular ag-grid

我想在填充数据之前清除ag-grid。由于rowModelType设置为' infinite',我无法使用setRowData()方法更改网格数据。相反,我创建了另一个设置空数据的本地数据源,然后将其用作网格的数据源。我使用下面的代码:

clearGrid(){
    let self = this;
    let dataSource = {
       getRows(params:any) {
          params.successCallback([],0);
       }
    };
    this.gridOptions.api.setDatasource(dataSource);
}

我在这个链接中找到了它:    Clear data in ag-grid

然而,当我使用上面的clear()函数清除网格时,看起来我无法填充任何数据,或者至少在网格中看不到数据。

这是我的代码,用于清除然后填充我的component.ts中的数据:

public populateData(formID: string) {

    //Clear the grid first:
    this.clearGrid();

    // Create columns
    this.createColumnDefs(); 

    // Get rows from database and display them in grid:
    this.gridOptions.datasource = this.dataSource;

    // Refresh the grid:     
    this.gridOptions.api.refreshInfiniteCache();  

}

如果我从代码中删除clearGrid(),一切正常,但网格中的数据将不会被清除,因此旧数据将存在,如果新记录的数量将少于旧记录的数量,您仍然可以在网格中看到它们。

如果有人可以解释我需要使用这个clearGrid()程序以及如何在清理数据后将ag-grid恢复到正常行为,我感激不尽。

非常感谢您的光临!

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。以下是我可能遇到同样问题的更正版本:

public populateData(formID: string) {

   //Clear the grid first:
   this.clearGrid();

   // Create columns
   this.createColumnDefs(); 

   // Wrote a function that repeats all what needs to be done 
   // in order to getting the rows and display them in the grid 
   // and called it here:  
   this.populateGrid();

   // Do not refresh the grid!     
   //Also do not refresh the grid using statements like this one: 
   //this.gridOptions.api.refreshInfiniteCache();
   // It throws an error and asks for adding using setTimeout.
}