切换react-bootstrap-table2上的列

时间:2019-04-15 07:43:21

标签: reactjs react-bootstrap-table

使用此库https://react-bootstrap-table.github.io/react-bootstrap-table2/

这可以切换列:https://react-bootstrap-table.github.io/react-bootstrap-table2/storybook/index.html?selectedKind=Bootstrap%204&selectedStory=Column%20Toggle%20with%20bootstrap%204&full=0&addons=1&stories=1&panelRight=0&addonPanel=storybook%2Factions%2Factions-panel

文档栏切换:https://react-bootstrap-table.github.io/react-bootstrap-table2/docs/basic-column-toggle.html

我需要知道隐藏了哪些列。

为此包含了一个回调:

onColumnToggle: Call this method when user toggle a column.

已实施:

<ToolkitProvider
          keyField="globalId"
          data={ this.props.data }
          columns={ this.state.columns }
          columnToggle
        >
          {
            props => {
              return (
                <>
                  <ToggleList {...props.columnToggleProps} onColumnToggle={this.columnToggle} className="d-flex flex-wrap"/>
                  <hr/>
                  <BootstrapTable
                    striped
                    bootstrap4
                    keyfield="globalId"
                    {...props.baseProps}
                  />
                </>
              )

            }
          }
        </ToolkitProvider>

我的函数this.columnToggle会按预期触发。但是表本身不再隐藏/显示列。如果我删除了功能,它将再次起作用。

更新: columnToggle函数:

 columnToggle = (column) => {
    console.log(column); // outputs the toggled column
  };

1 个答案:

答案 0 :(得分:1)

ToggleList使用渲染道具设计模式,因此它发送原始的onColumnToggle 使用您在组件props上展开的ToggleList,而且,您还提供了自己的onColumnToggle函数副本,它将覆盖预期的结果。

一个简单的解决方案,因此您可以通过执行以下操作来利用这两个功能(组件的实际onColumnToggle及其副本):

<ToggleList {...props.columnToggleProps} onColumnToggle={() => {this.columnToggle(); props.columnToggleProps.onColumnToggle(/* whatever params it needs */)}} className="d-flex flex-wrap"/>

这将允许您在切换列时进行自定义操作,并且仍然具有ToggleList API的原始功能。

编辑:该解决方案的问题在于ToggleList组件似乎不受控制。因此,我建议您使用官方文档中的example