【问题标题】:ag-grid react: gridApi is undefinedag-grid 反应:gridApi 未定义
【发布时间】:2019-11-15 09:13:42
【问题描述】:

我正在尝试将 Grid API 中的函数 sizeColumnsToFit() 用于我的 ag-grid 表。调用此函数时,我收到错误“TypeError: Cannot read property 'sizeColumnsToFit' of undefined”

我正在尝试重新创建此链接中的示例: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>
);
}

【问题讨论】:

    标签: reactjs ag-grid-react


    【解决方案1】:

    如果 this.gridApi 在渲染函数之外或在 ag 网格的函数内,则 this.gridApi 似乎超出了 AgGridReact 对象的范围。代替上面的配置,我在onGridReady函数中调用了这个函数,效果很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2021-04-23
      • 2020-03-10
      • 2021-05-20
      • 2018-01-16
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多