【问题标题】:Ant Design for React : Show/Hide particular columnAnt Design for React:显示/隐藏特定列
【发布时间】:2020-10-26 08:01:06
【问题描述】:

我需要一些帮助,在 Ant Design 表中,我需要根据状态值隐藏/显示表的特定列。在给定的沙箱链接中,我需要在开关关闭时隐藏姓氏列,并在开关打开时显示。

请有人调查一下,帮帮我。

参考:https://codesandbox.io/s/purple-sun-1rtz1?file=/index.js

【问题讨论】:

    标签: reactjs state antd


    【解决方案1】:

    有一个工作代码,但它应该根据您的需要进行更多的自定义、交互和重构:

    // You can also modify the data in the `handleChnage`
    // Or conditionally display it like here:
    
    class EditableTable extends React.Component {
      state = {
        surNameShow: false
      };
      constructor(props) {
        super(props);
        this.columns = [
          {
            title: "Name",
            dataIndex: "name",
            width: "30%"
          },
          {
            title: "Surname",
            dataIndex: "surname",
            width: "30%"
          }
        ];
        this.state = {
          dataSource: [
            {
              key: "0",
              name: "Edward 1",
              surname: "King 1"
            },
            {
              key: "1",
              name: "Edward 2",
              surname: "King 2"
            }
          ]
        };
      }
      handleChnage = key => {
        this.setState({ surNameShow: !this.state.surNameShow }, () => {
          console.log(this.state.surNameShow);
        });
      };
    
      render() {
        const { dataSource } = this.state;
        const columns = this.columns;
        return (
          <div>
            <p className="mr-3"> Show surname</p>
            <Switch onChange={() => this.handleChnage()} />
            <Table
              bordered
              dataSource={dataSource}
              columns={
                this.state.surNameShow
                  ? columns
                  : columns.filter(ea => ea.dataIndex !== "surname")
              }
              pagination={false}
            />
          </div>
        );
      }
    }
    
    ReactDOM.render(<EditableTable />, document.getElementById("container"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多