如何将ag-grid-react包装到组件中?

时间:2018-07-26 14:18:17

标签: reactjs redux ag-grid-react

我在Redux上使用React。我有一张用ag-grid制成的桌子用来反应。 表格数据可以直接编辑并保存。

如何为该表创建包装器组件?我需要具有功能相同的多个表,只是数据不同。

在ag-grid documentation中,它显示了如何使用redux store和reducers实施,但是该解决方案仅适用于一个表,因为每个应用程序只有一个状态。我不想因为我有两个不同的数据列表而将我的代码复制一遍...

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子

// Grid.jsx
import React, {
    PureComponent,
} from 'react';

import {
    AgGridReact,
} from 'ag-grid-react';

class Grid extends PureComponent {
    state = {
        columnDefs : [],
        rowData : []
    };


    /**
     * This will set your internal columnDefs & rowData
     * from the props being passed in
     * <Grid columnDefs={columnDefs} rowData={rowData} />
     */

    static getDerivedStateFromProps(nextProps) {

        const {
            columnDefs,
            rowData,
        } = nextProps;

        return {
            columnDefs,
            rowData,
        };
    }

    render() {
        const {
            columnDefs,
            rowData,
        } = this.state;

        return (

            <AgGridReact
                columnDefs={columnDefs}
                rowData={rowData}

            />
        );
    }
}

export default Grid;

// ExampleImpl.jsx
import Grid from './Grid';
class ExampleImpl extends PureComponent {
    render() {
        // Assuming you got these from the connect
        const rowData: [
            {
              name: "Ireland",
              continent: "Europe",
              language: "English",
              code: "ie",
              population: 4000000,
              summary: "Master Drinkers"
            },
          ];
        const columnDefs: [
            {
              headerName: "Name",
              field: "name",
              cellRenderer: countryCellRenderer
            },
            {
              headerName: "Continent",
              field: "continent",
              width: 150
            },

            {
              headerName: "Language",
              field: "language",
              width: 150
            }
        ];
        return <Grid
            rowData = {rowData}
            columnDefs = {columnDefs}
        />
    }
}

export default connect(ExampleImpl);

// ExampleImplII.jsx
import Grid from './Grid';
class ExampleImplII extends PureComponent {
    render() {
        // Assuming you got these from connect
        const rowData: [
            {
              name: "Ireland",
              continent: "Europe",
              language: "English",
              code: "ie",
              population: 4000000,
              summary: "Master Drinkers"
            },
          ];
        const columnDefs: [
            {
              headerName: "Name",
              field: "name",
              cellRenderer: countryCellRenderer
            },
            {
              headerName: "Continent",
              field: "continent",
              width: 150
            },
            {
              headerName: "Language",
              field: "language",
              width: 150
            }
        ];
        return <Grid
            rowData={rowData}
            columnDefs={columnDefs}
        />
    }
}

export default connect(ExampleImplII);

这样,您将拥有1个表示性组件和2个容器组件(智能组件),它们知道如何获取数据并将其传递给表示性组件。