ag-grid反应:gridApi未定义

时间:2019-07-04 15:40:49

标签: reactjs ag-grid-react

我正在尝试将Grid API的函数sizeColumnsToFit()用于我的ag-grid表。调用此函数时,出现错误“ TypeError:无法读取未定义的属性'sizeColumnsToFit”

我正在尝试基本上重新创建此链接中的示例:https://www.ag-grid.com/javascript-grid-resizing/,但似乎onGridReady函数未正确定义this.gridApi

...
onGridReady = (params) => {
    this.api = params.api;
    this.columnApi = params.columnApi;
    this.gridApi = params.gridApi;
    this.gridColumnApi = params.gridApi;
  }

render(){
...
//defining columns and rows
this.gridApi.sizeColumnsToFit();
return (
    <div
    className="ag-theme-balham"
style={{
      height: '100%',
      width: '100%          
    }}>

<AgGridReact
  columnDefs={columns}
      rowData={rows}
      onGridReady= {this.onGridReady}>
    </AgGridReact>
</div>
);
}

1 个答案:

答案 0 :(得分:0)

如果this.gridApi在render函数之外或在ag网格的函数之内,则似乎超出了AgGridReact对象的范围。代替上面的配置,我在onGridReady函数中调用了该函数,它工作得很好!