AG-GRID调用gridApi的applyTransaction方法提示不存在该函数
AG Grid 报错gridApi不存在applyTransaction方法的解决方案
常见报错原因
- 版本不兼容:
applyTransaction是AG Grid v23.0.0版本才正式加入的API,如果你当前项目依赖的AG Grid版本低于23.0.0,会直接报该方法不存在的错误。 - 行模型不支持:
applyTransaction仅支持客户端行模型(Client Side Row Model),如果你配置gridOptions时指定了其他行模型(包括无限行模型、服务端行模型、Viewport行模型),gridApi上不会挂载该方法。 - API引用异常:虽然你已经在onGridReady中绑定了gridApi,可先在调用applyTransaction前打印
this.gridApi,确认该对象正常挂载且没有被其他逻辑重写覆盖。
对应解决方法
- 检查项目package.json中
ag-grid-community、ag-grid-react(React项目)的版本号,低于23.0.0的话升级到最新稳定版即可。 - 检查你的gridOptions配置,确认
rowModelType没有设置为serverSide/infinite/viewport,如果你的业务需要使用非客户端行模型,需要更换对应行模型的数据更新方式,不能使用applyTransaction。 - 调用前增加判断逻辑,避免异常调用报错,也可以补充降级逻辑兼容场景:
const sel = this.gridApi.getSelectedRows(); console.log(sel); // 增加兼容性判断 if (this.gridApi && typeof this.gridApi.applyTransaction === 'function') { const res = this.gridApi.applyTransaction({ remove: sel }); } else { // 降级处理:全量更新rowData实现相同效果 const currentRowData = []; this.gridApi.forEachNode(node => currentRowData.push(node.data)); const newRowData = currentRowData.filter(item => !sel.includes(item)); this.gridApi.setRowData(newRowData); }
内容的提问来源于stack exchange,提问作者UnskilledCoder
相关产品推荐
相关产品推荐

