我正在尝试使用antd表进行rowselection reactjs。 ant-components-table-demo-expand-children
当我选择父行时,它必须选择该父行的子行(应在子行中打勾)。
this.rowSelection = {
onSelect: (record, selected, selectedRows) => this.onSelectChange(record, selected, selectedRows),
onSelectAll: (selected, selectedRows, changeRows) => this.allRowsSelected(selected, selectedRows, changeRows)
};
<Table
rowKey={data._id}
columns={this.columns1}
rowSelection={this.rowSelection}
expandedRowRender={(record, index, indent, expanded) =>
this.expanding(record, expanded)
}
onExpand={this.onExpand}
dataSource={data}
/>
答案 0 :(得分:1)
选择parent
节点时,可以从children
函数中获取onSelect
节点。同样,您可以从select/deselect
函数onSelectAll
获取所有节点。
您需要将所选keys
节点中的parent
个节点及其子节点存储在state
中。
要select/deselect
复选框,您需要像这样将keys
设置/取消设置为selectedRowKeys
:selectedRowKeys: Array.from(selectedCBKeys)
(selectedCBKeys =父键+它是子键)
const rowSelection = {
selectedRowKeys: Array.from(selectedCBKeys),
onChange: (selectedRowKeys, selectedRows) => {},
onSelect: (record, selected, selectedRows) => {
this.getKey(record);
},
onSelectAll: (selected, selectedRows, changeRows) => {
changeRows.map(item => this.getKey(item));
}
};
如何获取父树的子节点的所有键?
只需递归遍历父树即可。我这样收集了所有keys
:
getKey = data => {
this.storeKey(data.key); //it just stores the key in "state"
if (data.children) {
data.children.map(item => this.getKey(item));
}
};
这就是我存储所有密钥的方式。我使用了Set。
state = {
selectedCBKeys: new Set()
};
storeKey = key => {
//If the key is already present in "state" just delete it.
//It helps in toggling the checkboxes.. right?
if (this.state.selectedCBKeys.has(key)) {
const newSet = this.state.selectedCBKeys;
newSet.delete(key);
this.setState({
selectedCBKeys: newSet
});
return;
}
this.setState(prevState => ({
...prevState,
selectedCBKeys: prevState.selectedCBKeys.add(key)
}));
};
App.js
import { Table } from "antd";
const columns = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Age",
dataIndex: "age",
key: "age",
width: "12%"
},
{
title: "Address",
dataIndex: "address",
width: "30%",
key: "address"
}
];
const data = [
{
key: 1,
name: "John Brown sr.",
age: 60,
address: "New York No. 1 Lake Park",
children: [
{
key: 11,
name: "John Brown",
age: 42,
address: "New York No. 2 Lake Park"
},
{
key: 12,
name: "John Brown jr.",
age: 30,
address: "New York No. 3 Lake Park",
children: [
{
key: 121,
name: "Jimmy Brown",
age: 16,
address: "New York No. 3 Lake Park"
}
]
}
]
},
{
key: 2,
name: "Joe Black",
age: 32,
address: "Sidney No. 1 Lake Park"
}
];
class App extends Component {
state = {
selectedCBKeys: new Set()
};
storeKey = key => {
if (this.state.selectedCBKeys.has(key)) {
const newSet = this.state.selectedCBKeys;
newSet.delete(key);
this.setState({
selectedCBKeys: newSet
});
return;
}
this.setState(prevState => ({
...prevState,
selectedCBKeys: prevState.selectedCBKeys.add(key)
}));
console.log("new keys: ", key);
};
getKey = data => {
this.storeKey(data.key);
if (data.children) {
data.children.map(item => this.getKey(item));
}
};
render() {
const { selectedCBKeys } = this.state;
// rowSelection objects indicates the need for row selection
const rowSelection = {
selectedRowKeys: Array.from(selectedCBKeys),
onChange: (selectedRowKeys, selectedRows) => {
console.log(
`selectedRowKeys: ${selectedRowKeys}`,
"selectedRows: ",
selectedRows
);
},
onSelect: (record, selected, selectedRows) => {
this.getKey(record);
},
onSelectAll: (selected, selectedRows, changeRows) => {
console.log(
"onSelectAll: ",
selected,
" selectedRows: ",
selectedRows,
" changeRows: ",
changeRows
);
// selectedRows.map(item => this.getKey(item));
changeRows.map(item => this.getKey(item));
}
};
return (
<div className="parent">
<Table
columns={columns}
rowSelection={rowSelection}
dataSource={data}
/>
</div>
);
}
}
这是堆叠闪电战中的demo。让我知道,
答案 1 :(得分:0)
从版本 4.4.0
开始,更简单的单行解决方案是将 checkStrictly
设置为 false
。
查看更多here演示及其规范,(Ctrl
+F
并搜索关键字“checkStrictly”,演示及其文档都在那里)< /em>
rowSelection={{
checkStrictly: false,
onChange: (_, selectedRows) => {
setSelectedRows(selectedRows);
},
}}