【问题标题】:Adding colDefs dynamically动态添加 colDefs
【发布时间】:2021-11-11 23:18:55
【问题描述】:

我正在尝试在单击按钮时以编程方式添加列定义,而不是在我的 ReactJS 页面中对其进行硬编码。

{
    headerName: "Product1",
    resizable: true,
    wrapText: true,
    cellStyle: {
        'white-space': 'normal'
    },
    autoHeight: true,
    hide: true,
    cellRendererFramework.MyCustomColumnRenderer
}

不确定如何实施? 感谢您的帮助。

【问题讨论】:

标签: reactjs ag-grid


【解决方案1】:

您可以在网格中定义 columnDefs 以使用状态,然后动态设置状态。

import React from 'react';
import { render } from 'react-dom';
import { AgGridColumn, AgGridReact } from 'ag-grid-react';

import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';

const App = () => {
  const rowData = [
    { make: 'Toyota', model: 'Celica', price: 35000 },
    { make: 'Ford', model: 'Mondeo', price: 32000 },
    { make: 'Porsche', model: 'Boxter', price: 72000 },
  ];

  // use the colDefs state to define the column definitions
  const [colDefs, setColDefs] = React.useState([{ field: 'make' }]);

  // when the button is pressed set the state to cause the grid to update
  const handleAddColumns = ()=> {
    const dynamicFields = [
      { field: 'make', header: 'Car Make' },
      { field: 'model', sortable: true },
      { field: 'price' },
    ];
    setColDefs(dynamicFields);
  }

  return (
    <div>
      <button onClick={handleAddColumns}>Add Column Defs</button>
      <div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
        <AgGridReact 
            rowData={rowData}
            columnDefs={colDefs}>
        </AgGridReact>
      </div>
    </div>
  );
};

render(<App />, document.getElementById('root'));

另一种方法是使用Api的setColumnDef方法:

import React from 'react';
import { render } from 'react-dom';
import { AgGridColumn, AgGridReact } from 'ag-grid-react';

import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';

const App = () => {
  const rowData = [
    { make: 'Toyota', model: 'Celica', price: 35000 },
    { make: 'Ford', model: 'Mondeo', price: 32000 },
    { make: 'Porsche', model: 'Boxter', price: 72000 },
  ];

  // using state to define the columns initially
  const [colDefs, setColDefs] = React.useState([{ field: 'make' }]);
  // get a reference to the API when the onGridReady is fired
  // see the Grid definition in the JSX
  const [gridApi, setGridApi] = React.useState([]);

const handleAddColumns = ()=> {
    const dynamicFields = [
      { field: 'make', header: 'Car Make' },
      { field: 'model', sortable: true },
      { field: 'price' },
    ];
    // use the API to set the Column Defs
    gridApi.setColumnDefs(dynamicFields);
  }

  return (
    <div>
      <button onClick={handleAddColumns}>Add Column Defs</button>
      <div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
        <AgGridReact 
            rowData={rowData}
            columnDefs={colDefs}
            onGridReady={ params => {setGridApi(params.api)} }
            ></AgGridReact>
      </div>
    </div>
  );
};

render(<App />, document.getElementById('root'));

文档中的示例应该会有所帮助:

还有一篇来自 AG Grid 的博客文章涵盖了动态列定义:

【讨论】:

    【解决方案2】:

    使用setColumnDefs(columnDefs)

        const columnDefs = getColumnDefs();
        columnDefs.forEach(function (colDef, index) {
          colDef.headerName = 'Abcd';
        });
        this.gridApi.setColumnDefs(columnDefs);
    

    https://plnkr.co/edit/0ctig4P2yzPjhycB

    【讨论】:

    • 谢谢。如何使用上述方法动态添加单元格渲染器?cellRendererFramework.MyCustomColumnRenderer
    • colDef['cellRenderer'] = 'MyCustomColumnRenderer';然后在 frameworkComponents 中注册它。 ag-grid.com/react-data-grid/component-cell-renderer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2014-02-22
    • 2016-01-23
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多