简体   繁体   English

单击标题复选框时重置 antd 表行选择

[英]Reset antd table row selections when click on the header checkbox

So i have an antd table implemented with select/deselect all checkbox option.所以我有一个使用选择/取消选择所有复选框选项实现的 antd 表。 What i want to do if the user has selected only several of the checkboxes and then when clicking on the checkbox at header, the selected checkboxes should reset.如果用户只选择了几个复选框,然后单击标题处的复选框时,我想做什么,所选的复选框应该重置。 Current behaviour is it select all the checkboxes(rows).当前行为是选择所有复选框(行)。

Following is my rowSelection code以下是我的rowSelection代码

const rowSelection = {
    onSelectAll: (selected: any, selectedRows: any, changeRows: any) => {
      if(selectedRows.length !== 15){
        console.log("reset checkboxes");
      }
    },
    onChange: (selectedRowKeys: any, selectedRows: any) => {
      setSelectedRows(selectedRows);
    },
    getCheckboxProps: (record: any) => ({
      disabled: record.name === "Disabled User",
      name: record.name,
    }),
  };

One solution is to make the selected row controlled.一种解决方案是使选定的行受到控制。

const [selectedRowKeys, setSelectedRowKeys] = useState([]);

const rowSelection = {
    selectedRowKeys: selectedRowKeys,
    onSelectAll: (selected, selectedRows, changeRows) => {
      if (selectedRowKeys.length !== 0) {
        setSelectedRowKeys([]);
      }
    },
    onChange: (selectedRowKeys, selectedRows) => {
      setSelectedRowKeys(selectedRowKeys);
    }
};

When you click checkbox on rows, the onChange function will execute and set the selected row keys.当您单击行上的复选框时, onChange函数将执行并设置选定的行键。 And when you click checkbox at header, the onChange function will execute again and set all the row keys followed by onSelectAll (take note that setState on onChange will not take effect immediately) which then reset all the selected row keys if there are several selected row (on state).当你点击header处的checkbox时, onChange函数将再次执行并设置所有行键,然后是onSelectAll (注意onChange上的setState不会立即生效),如果有多个选中的行,则重置所有选中的行键(在状态)。

See working code here:在此处查看工作代码:

编辑 prod-http-h73fi

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

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