当我们使用antd表中的rowSelection在父行上进行选择时,选择子行

时间:2019-09-17 12:45:11

标签: reactjs antd

我正在尝试使用antd表进行rowselection reactjs。 ant-components-table-demo-expand-children

当我选择父行时,它必须选择该父行的子行(应在子行中打勾)。

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}
/>

2 个答案:

答案 0 :(得分:1)

select-all-children-checkboxes

选择parent节点时,可以从children函数中获取onSelect节点。同样,您可以从select/deselect函数onSelectAll获取所有节点。

您需要将所选keys节点中的parent个节点及其子节点存储在state中。

select/deselect复选框,您需要像这样将keys设置/取消设置为selectedRowKeysselectedRowKeys: Array.from(selectedCBKeys)(selectedCBKeys =父键+它是子键)

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));
      }
    };

如何获取父树的子节点的所有键?

只需递归遍历父树即可。我这样收集了所有keys

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

这就是我存储所有密钥的方式。我使用了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>
    );
  }
}

这是堆叠闪电战中的demo。让我知道,

答案 1 :(得分:0)

从版本 4.4.0 开始,更简单的单行解决方案是将 checkStrictly 设置为 false

查看更多here演示及其规范,Ctrl+F并搜索关键字“checkStrictly”,演示及其文档都在那里)< /em>

rowSelection={{
  checkStrictly: false,
  onChange: (_, selectedRows) => {
    setSelectedRows(selectedRows);
  },
}}