设置道具“ on​​GridReady”或“ rowSelection”时,“无法将未定义或null转换为对象”(反应)

时间:2019-11-18 04:10:04

标签: reactjs ag-grid

我尝试按照https://www.ag-grid.com/react-grid/

上的步骤进行操作

当我这样编码时:

<AgGridReact
 rowSelection="multiple"
....

<AgGridReact
   onGridReady={ params => this.gridApi = params.api } 
...

发生错误:

Uncaught TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at agGridColumn.js:63
    at Array.reduce (<anonymous>)
    at Function.umi../node_modules/@ag-grid-community/react/lib/agGridColumn.js.AgGridColumn.assign (agGridColumn.js:62)
    at Function.umi../node_modules/@ag-grid-community/react/lib/agGridColumn.js.AgGridColumn.createColDefFromGridColumn (agGridColumn.js:55)
    at Function.umi../node_modules/@ag-grid-community/react/lib/agGridColumn.js.AgGridColumn.toColDef (agGridColumn.js:38)
    at agGridColumn.js:34

我在[ag-grid]标签中检查了先前的问题,似乎没有人遇到过这种情况。 我该怎么解决?

1 个答案:

答案 0 :(得分:1)

还需要添加以下行:-

<div style={{ height: '150px', width: '600px' }} className="ag-theme-balham">
 <button onClick={this.onButtonClick}>Get selected rows</button>
</div>
<AgGridReact
    onGridReady={ params => this.gridApi = params.api }
....

并定义如下的onButtonClick()方法:-

onButtonClick = e => {
const selectedNodes = this.gridApi.getSelectedNodes()
const selectedData = selectedNodes.map( node => node.data )
const selectedDataStringPresentation = selectedData.map( node => node.make + ' ' + node.model).join(', ')
alert(`Selected nodes: ${selectedDataStringPresentation}`)
}

在此方法中,我们访问了Grid API对象引用以访问当前选择的网格行节点。(As per documentation