在ant-design表组件中仅选择一行

时间:2019-05-24 19:13:43

标签: javascript arrays reactjs checkbox antd

我在这里遇到问题。对我的项目来说,用户蚂蚁设计是必不可少的。链接在这里:https://ant.design/components/table 我必须使用的组件是表组件。当我单击一行时,我必须调度一个动作。我面临的问题是我必须只检查一行。 因此,当我单击一行时,应取消选择上一行中的复选框。

我尝试添加一些逻辑,如您在此处看到的那样:

class EvaluationDataTable extends React.Component {
  state = {
    selectedRowKeys: [], // Check here to configure the default column
  };

  onSelectChange = selectedRowKeys => {
    if (selectedRowKeys.length > 1) {
      const lastSelectedRowIndex = [...selectedRowKeys].pop();
      this.setState({ selectedRowKeys: lastSelectedRowIndex });
    }
    this.setState({ selectedRowKeys });
    console.log('selectedRowKeys changed: ', selectedRowKeys);
  };

  render() {
    const { selectedRowKeys } = this.state;
    const rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectChange,
    };
    return (
      <React.Fragment>
        <div style={{ marginBottom: 16 }} />
        <Table
          rowSelection={rowSelection}
          columns={columnEvaluation}
          dataSource={result}
        />
      </React.Fragment>
    );
  }
}```

But even if the selectedRow has the last checked row the no checkbox is checked. Any ideas?

1 个答案:

答案 0 :(得分:1)

您似乎可以将ant design表配置为使用单选按钮而不是复选框。由于单选按钮仅用于单选,因此应该可以解决您的用例。该文档表明antd Table组件具有一个rowSelection属性,该属性接受一个定义为here的配置对象。选项之一是type,您可以将其设置为radiocheckbox。您需要radio。所以你想要类似的东西:

<Table rowSelection={{type:'radio'}} >...</Table>
相关问题