简体   繁体   English

如何切换antd表的一行中的复选框?

[英]How to toggle a checkbox in a row of a antd table?

I have a checkbox in the first column of each row in the table. 我在表格的每一行的第一列中都有一个复选框。 I can toggle the checkbox by clicking it. 我可以点击它来切换复选框。

例

How do I properly, "antd way", make it toggle by clicking in any area of the whole row? 我如何正确地,“antd way”,通过点击整行的任何区域来切换?

It is there in antd documentation, but you need to take a state to handle it. 它存在于antd文档中,但您需要采用状态来处理它。

Rows can be selectable by making first column as a selectable column. 通过将第一列作为可选列,可以选择行。

You can use 您可以使用

onRow={record => ({
  onClick: () => {
    this.selectRow(record);
  }
})}

and in selectRow 并在selectRow中

selectRow = record => {
  const selectedRowKeys = [...this.state.selectedRowKeys];
  if (selectedRowKeys.indexOf(record.key) >= 0) {
    selectedRowKeys.splice(selectedRowKeys.indexOf(record.key), 1);
  } else {
    selectedRowKeys.push(record.key);
  }
  this.setState({ selectedRowKeys });
}

Demo and Source 演示来源

There is an onRow property that you can use. 您可以使用onRow属性。 See the example from the official documentation: 请参阅官方文档中的示例:

class App extends React.Component {
  state = {
    selectedRowKeys: [],
  };

  selectRow = (record) => {
    const selectedRowKeys = [...this.state.selectedRowKeys];
    if (selectedRowKeys.indexOf(record.key) >= 0) {
      selectedRowKeys.splice(selectedRowKeys.indexOf(record.key), 1);
    } else {
      selectedRowKeys.push(record.key);
    }
    this.setState({ selectedRowKeys });
  }

  onSelectedRowKeysChange = (selectedRowKeys) => {
    this.setState({ selectedRowKeys });
  }

  render() {
    const { selectedRowKeys } = this.state;

    const rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectedRowKeysChange,
    };

    return (
      <Table
        rowSelection={rowSelection}
        columns={columns}
        dataSource={data}
        onRow={(record) => ({
          onClick: () => {
            this.selectRow(record);
          },
        })}
      />
    );
  }
}

Source 资源

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

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