【问题标题】:ag-grid callback customizationsag-grid 回调自定义
【发布时间】:2019-08-11 08:08:00
【问题描述】:

我有一个包含多个网格的页面,可以由用户创建和自定义。每次用户创建网格时,我都会为其分配一个标识符并将其存储在我的内部状态中(标识符映射 -> 状态)

现在我想开始保存这些网格的状态,所以一直在研究回调,例如 onColumnVisible

这很有效(伪代码)

render() {
    for each 'identifier' {
        <DataGrid 
            // lots of other stuff
            onColumnVisible={this.saveColEvent}
        />
    }
}

@boundMethod
private saveColEvent(event: ColumnVisibleEvent) {
    const colState: any = event.columnApi.getColumnState();
    // TODO: set the colstate based on the identifier
}

我的问题是如何传递额外的参数 - 标识符 - 我需要在 saveColEvent 中索引到地图。

谢谢!

编辑:更准确的迭代-

const stateMap: Map<string, MyState> = this.props.stateMapping;
stateMap.forEach(
            (value: MyState, identifier: string) => {
            <div key={identifier}
                <DataGrid 
                    // lots of other stuff
                    onColumnVisible={this.saveColEvent}
                />
            </div>
            });

【问题讨论】:

  • 是的,对不起,我一开始不想用太多代码弄乱,但它是一个变量,例如见编辑。感谢收看。

标签: reactjs ag-grid


【解决方案1】:

我明白了。

const stateMap: Map<string, MyState> = this.props.stateMapping;
stateMap.forEach(
            (value: MyState, identifier: string) => {
            <div key={identifier}
                <DataGrid 
                    // lots of other stuff
                    onColumnVisible={() => this.saveColEvent(identifier)}
                />
            </div>
            });


@boundMethod
private saveColEvent(identifier) {
    // this is free, you can mention identifier at any time
    const colState: any = event.columnApi.getColumnState();
}

我想这可能是你想要的。注意:这是未经测试的。如果它不起作用,请告诉我。

所以我正在做的是添加一个identifier 并将标准函数更改为箭头函数。

【讨论】:

  • 谢谢!这让我走上了正轨。如果我完全使用您的代码,则该事件丢失,但我可以通过执行以下操作将其取回:(e) => this.saveColEvent(e, identifier),然后将其添加回 saveColEvent 中的第一个参数。甜蜜的..现在我有我需要的两条信息。
猜你喜欢
  • 2020-08-31
  • 2020-03-31
  • 2019-05-11
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
相关资源
最近更新 更多