gpt4 book ai didi

javascript - AntD rowSelection 功能组件

转载 作者:行者123 更新时间:2023-12-03 23:44:12 24 4
gpt4 key购买 nike

我正在尝试获取表中的单个元素键。
但我得到未定义我怎样才能捕获 id?
https://ant.design/components/table/#components-table-demo-expand-children

const [select, setSelect] = useState({
selectedRowKeys: [],
loading: false,
});

console.log("selectedRowKeys", select);

const { selectedRowKeys, loading } = select;

const rowSelection = {
selectedRowKeys,
onChange: (selectedRowKeys) => {
setSelect({
...select,
selectedRowKeys: [...select.selectedRowKeys, selectedRowKeys],
});
},
};

return (
<div>
<Table
columns={columns}
rowSelection={rowSelection}
dataSource={dataSource}
loading={!props.employeeList}
/>
</div>);
Here's a screenshot of console.log()

最佳答案

您需要在 dataSource 的每个对象上添加一个 key prop大批

const dataSource = [
{
key: 1,
name: `Edward King 1`,
age: 32,
address: `London, Park Lane no. 1`
},
{
key: 2,
name: `Edward King 2`,
age: 35,
address: `London, Park Lane no. 2`
}
];
然后在你的 rowSelection您需要的对象删除此代码 [...select.selectedRowKeys, selectedRowKeys] ,如果您取消选择一个项目并再次选择它并导致重复,这将推送到状态。它应该是:
const rowSelection = {
selectedRowKeys,
onChange: (selectedRowKeys) => {
setSelect({
...select,
selectedRowKeys: selectedRowKeys
});
}
};
查看您的工作代码 here

关于javascript - AntD rowSelection 功能组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63918872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com