Expand a row by click of table on a certain condition Ant Design table react js

前端 未结 1 703
遥遥无期
遥遥无期 2021-01-23 22:00

I need a small help.

I need to write the code such that the table row should expand only when it\'s toggle is on when the toggle is off it should not expand. I have used

相关标签:
1条回答
  • 2021-01-23 22:10

    just paste this code . it is working . Hope this'll help you .

    import React from "react";
    import ReactDOM from "react-dom";
    import "antd/dist/antd.css";
    import "./index.css";
    import { Table, Switch } from "antd";
    
    const { Column } = Table;
    
    class EditableTable extends React.Component {
      state = {
        firstRow: false,
        secondrow: false
      };
      constructor(props) {
        super(props);
        this.state = {
          dataSource: [
            {
              key: "0",
              name: "Edward King 0",
              expandable: false
            },
            {
              key: "1",
              name: "Edward King 1",
              expandable: false
            }
          ]
        };
      }
      handleChnage = key => {
        let k = parseInt(key);
        let data = this.state.dataSource;
        let value = data[k].expandable;
        data[k].expandable = !value;
        this.setState({
          dataSource: data
        });
      };
    
      render() {
        const { dataSource } = this.state;
        return (
          <Table
            bordered
            dataSource={dataSource}
            pagination={false}
            expandRowByClick={true}
            expandable={{
              expandedRowRender: record => (
                <p style={{ margin: 0 }}>{"Here is expandable row"}</p>
              ),
              rowExpandable: record => record.expandable
            }}
          >
            <Column title="name" dataIndex="name" width="30%" editable={true} />
            <Column
              align="right"
              render={(text, record, index) => {
                return <Switch onChange={() => this.handleChnage(record.key)} />;
              }}
            />
          </Table>
        );
      }
    }
    
    ReactDOM.render(<EditableTable />, document.getElementById("container"));
    
    0 讨论(0)
提交回复
热议问题