Select child rows when we select on the parent row using rowSelection in antd table

廉价感情. 提交于 2019-12-11 07:38:38

问题


I'm trying rowselection reactjs using antd table.I'm trying this one ant-components-table-demo-expand-children

when I select parent row it must select child rows of that parent (it should tick mark the child rows).

this.rowSelection = {
    onSelect: (record, selected, selectedRows) => this.onSelectChange(record, selected, selectedRows),
    onSelectAll: (selected, selectedRows, changeRows) => this.allRowsSelected(selected, selectedRows, changeRows)
};


<Table
    rowKey={data._id}
    columns={this.columns1}
    rowSelection={this.rowSelection}
    expandedRowRender={(record, index, indent, expanded) =>
        this.expanding(record, expanded)                         
    }
    onExpand={this.onExpand}
    dataSource={data}
/>

回答1:


When you select a parent node, you can get the children nodes from onSelect function. Similarly, You can get all the nodes when you select/deselect from onSelectAll function.

You need to store those keys of selected parent node and it's children nodes in state.

To select/deselect checkboxes, you need to set/unset those keys to selectedRowKeys like so: selectedRowKeys: Array.from(selectedCBKeys) (selectedCBKeys = parent key + it's children keys)

const rowSelection = {
      selectedRowKeys: Array.from(selectedCBKeys),

      onChange: (selectedRowKeys, selectedRows) => {},

      onSelect: (record, selected, selectedRows) => {
        this.getKey(record);
      }, 

      onSelectAll: (selected, selectedRows, changeRows) => {
        changeRows.map(item => this.getKey(item));
      }
    };

How to get all the keys of children nodes of a parent tree?

Just recursively traverse the parent tree. I collected all the keys like so:

getKey = data => {
    this.storeKey(data.key); //it just stores the key in "state"
    if (data.children) {
      data.children.map(item => this.getKey(item));
    }
  };

This is how I stored all the keys. I used Set.

 state = {
    selectedCBKeys: new Set()
  };

  storeKey = key => {

    //If the key is already present in "state" just delete it. 
     //It helps in toggling the checkboxes.. right?

    if (this.state.selectedCBKeys.has(key)) { 
      const newSet = this.state.selectedCBKeys;
      newSet.delete(key);
      this.setState({
        selectedCBKeys: newSet
      });
      return;
    }

    this.setState(prevState => ({
      ...prevState,
      selectedCBKeys: prevState.selectedCBKeys.add(key)
    }));
  };

App.js

import { Table } from "antd";

const columns = [
  {
    title: "Name",
    dataIndex: "name",
    key: "name"
  },
  {
    title: "Age",
    dataIndex: "age",
    key: "age",
    width: "12%"
  },
  {
    title: "Address",
    dataIndex: "address",
    width: "30%",
    key: "address"
  }
];

const data = [
  {
    key: 1,
    name: "John Brown sr.",
    age: 60,
    address: "New York No. 1 Lake Park",
    children: [
      {
        key: 11,
        name: "John Brown",
        age: 42,
        address: "New York No. 2 Lake Park"
      },
      {
        key: 12,
        name: "John Brown jr.",
        age: 30,
        address: "New York No. 3 Lake Park",
        children: [
          {
            key: 121,
            name: "Jimmy Brown",
            age: 16,
            address: "New York No. 3 Lake Park"
          }
        ]
      }
    ]
  },
  {
    key: 2,
    name: "Joe Black",
    age: 32,
    address: "Sidney No. 1 Lake Park"
  }
];

class App extends Component {
  state = {
    selectedCBKeys: new Set()
  };

  storeKey = key => {
    if (this.state.selectedCBKeys.has(key)) {
      const newSet = this.state.selectedCBKeys;
      newSet.delete(key);
      this.setState({
        selectedCBKeys: newSet
      });
      return;
    }
    this.setState(prevState => ({
      ...prevState,
      selectedCBKeys: prevState.selectedCBKeys.add(key)
    }));
    console.log("new keys: ", key);
  };

  getKey = data => {
    this.storeKey(data.key);
    if (data.children) {
      data.children.map(item => this.getKey(item));
    }
  };
  render() {
    const { selectedCBKeys } = this.state;
    // rowSelection objects indicates the need for row selection
    const rowSelection = {
      selectedRowKeys: Array.from(selectedCBKeys),
      onChange: (selectedRowKeys, selectedRows) => {
        console.log(
          `selectedRowKeys: ${selectedRowKeys}`,
          "selectedRows: ",
          selectedRows
        );
      },
      onSelect: (record, selected, selectedRows) => {
        this.getKey(record);
      },
      onSelectAll: (selected, selectedRows, changeRows) => {
        console.log(
          "onSelectAll: ",
          selected,
          " selectedRows: ",
          selectedRows,
          " changeRows: ",
          changeRows
        );
        // selectedRows.map(item => this.getKey(item));
        changeRows.map(item => this.getKey(item));
      }
    };
    return (
      <div className="parent">
        <Table
          columns={columns}
          rowSelection={rowSelection}
          dataSource={data}
        />
      </div>
    );
  }
}

Here is the demo on stackblitz. Let me know,



来源:https://stackoverflow.com/questions/57974511/select-child-rows-when-we-select-on-the-parent-row-using-rowselection-in-antd-ta

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!