在ag网格和angular2中清除数据

在ag网格和angular2中清除数据,angular,ag-grid,Angular,Ag Grid,我试图在填充数据之前清除ag网格。由于rowModelType设置为“无限”,因此无法使用setRowData()方法更改网格数据。相反,我创建了另一个本地数据源来设置空数据,然后将其用作网格的数据源。我使用了下面的代码: clearGrid(){ let self = this; let dataSource = { getRows(params:any) { params.successCallback([],0); }

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

clearGrid(){
    let self = this;
    let dataSource = {
       getRows(params:any) {
          params.successCallback([],0);
       }
    };
    this.gridOptions.api.setDatasource(dataSource);
}
我在这个链接中找到了:

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

以下是我的代码,用于清除并填充组件中的数据。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恢复正常行为,我将不胜感激


非常感谢你抽出时间

我发现了问题所在。以下是我为那些可能有相同问题的人提供的更正版本:

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.
}

我结束这个问题,因为我得到了更新部分中解释的解决方案。在空网格@answerd中加载数据需要做什么