【问题标题】:Toggle columns on react-bootstrap-table2在 react-bootstrap-table2 上切换列
【发布时间】:2019-09-05 03:53:12
【问题描述】:

使用这个库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
  };

【问题讨论】:

  • 你能发布更多的代码吗?或者也许是一个带有简化示例的代码笔?这样我们就不必花费所有时间弄清楚如何导入/使用ToolkitProviderBootstrapTable
  • 你能发布你的this.columnToggle函数吗?
  • @seanulus 我已经添加了 columnToggle 函数。

标签: reactjs react-bootstrap-table


【解决方案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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2022-11-10
  • 2021-07-02
  • 2020-08-30
  • 2019-12-23
  • 2018-08-08
  • 1970-01-01
相关资源
最近更新 更多