如何向 React-Data-Grid 添加多个新列?

How can i add more than one new Column to a React-Data-Grid?

嗨,对于一个项目,我需要用户 select 应该在 React 数据网格中显示的列。一些列可能是预设的,其他的可能会在以后添加。但是我试过了,即使相应地设置了状态并且没有显示错误,它也不能用于多个添加的列

新列将由 Selector.This 选择,然后添加到表类的状态中。 如果尝试通过强制渲染方法的 props 传递它,但它也不会多次更新列。 我使用了 different/same 键集。如果它是第一个新专栏,则将识别 Sames。 我尝试调试它是否有任何理由将 nextColumns 归类为未更改,但它并没有让我更接近。

state.columns while in render()

0: {key: "date", name: "Date"}   ->Preset
1: {key: "date", name: "Date1"}  ->Added and created
2: {key: "ffd", name: "Testfdg2"}->Added to state not created
3: {key: "test", name: "Test3"}  ->Not created
4: {key: "test", name: "Test4"}
5: {key: "test", name: "Test5"}
6: {key: "test", name: "Test6"}
length: 7
__proto__: Array(0)

组件调用

<ReactDataGrid
    columns={this.state.columns}
    rowGetter={i => this.handleRowGetter(i)}
    rowsCount={this.rowsCount()}
    minHeight={500}
    minColumnWidth={10}
    onGridSort={(sortColumn, sortDirection)=>this.gridSort(sortColumn, sortDirection)}
    toolbar={
      <Toolbar enableFilter={true}
    />}
    onAddFilter={this.handleFilterChange}
    onClearFilters={this.handleOnClearFilters}
    onRowDoubleClick={(click,row) =>{this.handleClick(click,row)}}

    />

所以目标是根据状态创建所有列

如果需要更多代码,我很乐意提供

你好乔纳森

来自docs

key: A unique key to distinguish each column

您需要为 key 使用不同的值。所以每个 key 都是 uniq

好的解决了

约反应数据中的 5804-grid.js

    if (!ColumnMetrics.sameColumns(this.props.columns, nextProps.columns,this.props.columnEquality) || nextProps.minWidth !== this.props.minWidth) {
          var columnMetrics = this.createColumnMetrics(nextProps);
          this.setState({ columnMetrics: columnMetrics });
       }

this.props.columns 将与下一个 props.columns 相同,因此不会更新 columnMetrics。

约反应数据中的 6500-grid.js

    this.setupGridColumns = function () {
    var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0]:_this2.props;
    var columns = props.columns;

    if (_this2._cachedColumns === columns) {
      return _this2._chachedComputedColumns;
    }

更多 _this2._cachedColumns 将与 props.columns 相同,因此 cachedComputedColumns 是 returned

所以 ColumnMetrics 没有真正的更新 -> 没有视觉更新。即使道具和状态设置正确。

因此排除 if 并更改 return 语句将使您可以向 Table

添加新列