javascript - ant-design table 组件中仅选择一行
问题描述
我这里有问题。我的项目必须使用 ant design。链接在这里: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?
解决方案
看起来您可以将 ant 设计表配置为使用单选按钮而不是复选框。由于单选按钮用于单选,这应该可以解决您的用例。文档表明 antd Table 组件有一个rowSelection
prop,它接受一个配置对象,定义在这里。其中一个选项是type
,您可以将其设置为radio
或checkbox
。你想要radio
。所以你想要这样的东西:
<Table rowSelection={{type:'radio'}} >...</Table>
推荐阅读
- html - Swift 使用 html-PDF
- vba - MS ACESS : 限制 ms 访问查询中的行数
- user-defined-functions - SnowFlake 中 SQL UDF 与 Javascript UDF 的性能对比
- c++ - clang 6 不支持 unordered_map::merge?
- c - strcpy() 函数进入无限循环
- linux - Increase the base device size of docker container with Kubernetes
- windows - 适用于 Mac 和 Windows 10 的 Git 在同一目录中
- mysql - 在同一个 mysql 会话中具有多个查询和重定向的 Bash 脚本
- java - 遍历节点类被另一个类继承的树
- xamarin.forms - 如何将背景图像设置为 xamarin.forms 中的网格?