繁体   English   中英

在 react-bootstrap-table2 上切换列

[英]Toggle columns on react-bootstrap-table2

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

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

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

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

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

编辑:此解决方案的问题,即ToggleList组件似乎不受控制。 所以我建议使用官方文档中的这个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM