【问题标题】:Clearing data in ag-grid and angular2清除 ag-grid 和 angular2 中的数据
【发布时间】:2018-02-07 07:52:03
【问题描述】:

我正在尝试在填充数据之前清除 ag-grid。由于 rowModelType 设置为“无限”,我无法使用 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 恢复正常行为,我将不胜感激。

非常感谢您的宝贵时间!

【问题讨论】:

  • 我关闭了这个问题,因为我得到了我在更新部分中解释的解决方案。

标签: angular ag-grid


【解决方案1】:

我发现了问题所在。这是我为可能遇到相同问题的人的更正版本:

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 中加载数据需要做什么
猜你喜欢
  • 2018-10-10
  • 2016-11-15
  • 2017-04-29
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 2018-05-09
  • 2021-04-16
  • 2017-09-26
相关资源
最近更新 更多