我正在使用 react antd 。我有一组对象是groupKey。我正在使用 Groupkey 映射复选框,并且我有两种不同类型的复选框。一种是全选复选框。当用户单击“全选”或“用户选择所有个人”复选框时,它实际上有效。另一个是单独的复选框,用户可以单独选择。当用户在 Button 上提交时,它会给我这个数据格式 ["manage_books","manage_journals","manage_deals"]
这是我的尝试代码:
let defaultCheckedList = ["manage_deals"];
state = {
groupKey: [{
id: 1,
key: "manage_books",
label: "books"
},
{
id: 2,
key: "manage_journals",
label: "journals"
},
{
id: 3,
key: "manage_deals",
label: "deals"
}
],
checkedList: defaultCheckedList,
output: [],
indeterminate: true,
checkAll: false
};
onCheckAllChange = e => {
this.setState({
checkedList: e.target.checked ?
this.state.groupKey.map(item => item.key) :
[],
indeterminate: false,
checkAll: e.target.checked
});
};
onChange = (e, value) => {
this.setState({
checked: e.target.checked,
output: this.state.output.concat(value)
});
};
onSubmit = () => {
console.log(this.state.output)
}
render(UI)
<
div >
<
div className = "site-checkbox-all-wrapper" >
Select All <
Checkbox
indeterminate = {
this.state.indeterminate
}
onChange = {
this.onCheckAllChange
}
checked = {
this.state.checkAll
}
/> <
/div>
I am looping checkbox by groupKey.I am passing key using onChange method. {
this.state.groupKey.map(item => ( <
div className = "userpermission-content"
key = {
item.id
} > {
item.label
} <
Checkbox onChange = {
(e, value) => this.onChange(e, item.key)
}
value = {
item.key
}
/>{" "} <
/div>
))
} <
button onClick = {
this.onSubmit
} > submit < /button> <
/div>
);
}
}
在这段代码中,你可以看到两个单独的复选框是初始选择,我需要完全像这样:https : //codesandbox.io/s/4k6qi
这是我的 codesanbox:https ://codesandbox.io/s/check-all-ant-design-demo-vhidd ? file =/ index.js