首页 > 解决方案 > ag-grid - 使用无限行模型添加行

问题描述

我需要在无限滚动的 ag-grid 中添加一个新行。有类似的问题,例如这个。但是我的情况非常不同。我在 ag-grid 中使用内联编辑,并有一个添加新行的按钮。有一个提交按钮,用户可能会也可能不会按下。因此,当新行添加到客户端站点时,不会立即修改服务器上的数据。当我使用客户端模型时,一切都很简单——我从服务器获取行,可以向数组添加新行并在 ag-grid 上调用 setRowData。但是,此功能不适用于无限滚动。如果这很重要,我正在使用 angular 2+。

我试图调用 applyTransaction 但在控制台中出现错误:

g-Grid:updateRowData() 仅适用于 ClientSideRowModel。使用 InfiniteRowModel 在 v23.1 中已弃用并在 v24.1 中删除

似乎 a-grid 只是删除了没有可替换的功能。

标签: ag-grid

解决方案


我个人没有使用带角度的 agGrid,但如果您使用的是infiniteRow模型,则可以使用setDataSource强制表格刷新数据的方法。

setDataSource方法接受具有两个最小参数的对象:

{
  rowCount: null,
  getRows: (params) =>{
    // here you can do your server call or, you can pass the data you want instead.  

  // you also need to do one thing at the end of your row generation, call this method
//`rows` is the array of rows you want to show, `totalRowsNumber` is the number of rows in total, including those that are not shown (the ones in later pages)
params.successCallback(rows, totalRowsNumber);
  
  } 
}

使用gridApi.setDatasource(dataSourceObject)


推荐阅读