取消选中React应用程序中未选中儿童元素的父复选框
我有一张选择全部检查的支票,以检查所有/取消选中所有子元素,但是当我取消选中任何子元素时,我也想取消选中父元的复选框。 当我选择全部选择时,我可以检查并取消选中,但是当没有选中任何子元素时,都无法实现副主席。
export const List = props => {
const{startOver} = props;
const onCheckAllcheckbox = (event) => {
let _items = props.items.map(item => {
item.isChecked = event.target.checked
return item;
});
props.setItems(_items);
}
const handleCheckChieldElement = (event) => {
let _items = props.items.map(item => {
if (item.id == event.target.value) {
item.isChecked = event.target.checked
}
return item;
})
props.setItems(_items);
}
return (
<Box>
{props.action && (
<Box horizontal align="center">
<input type="checkbox"
onChange={onCheckAllcheckbox}
defaultChecked={true}
style={{width: '20px', height: '15px'}} />
<Text>SELECT ALL</Text>
</Box>
)}
<div style={{overflow : 'auto', height : '200px', width : '900'}}>
<Box type="flat">
{props.items.map((item, index) => (
<Box key={index} horizontal align="center" style={{ margin: '.3rem 0' }}>
{props.action && (
<input type="checkbox"
onChange={handleCheckChieldElement}
checked={item.isChecked}
value={item.id}
style={{width: '20px', height: '15px'}} />
)}
{props.itemTemplate && props.itemTemplate(item) || (
<Text>{item.text}</Text>
)}
</Box>
))}
</Box>
</div>
</Box>
)
}
I have a check for Select All to check all/Uncheck all the child elements but when I uncheck any of the child elements I want to uncheck the parent checkbox as well.
I can check and uncheck all when I select the SELECT ALL, but cannot achieve vice versa when any of the child element is unchecked.
export const List = props => {
const{startOver} = props;
const onCheckAllcheckbox = (event) => {
let _items = props.items.map(item => {
item.isChecked = event.target.checked
return item;
});
props.setItems(_items);
}
const handleCheckChieldElement = (event) => {
let _items = props.items.map(item => {
if (item.id == event.target.value) {
item.isChecked = event.target.checked
}
return item;
})
props.setItems(_items);
}
return (
<Box>
{props.action && (
<Box horizontal align="center">
<input type="checkbox"
onChange={onCheckAllcheckbox}
defaultChecked={true}
style={{width: '20px', height: '15px'}} />
<Text>SELECT ALL</Text>
</Box>
)}
<div style={{overflow : 'auto', height : '200px', width : '900'}}>
<Box type="flat">
{props.items.map((item, index) => (
<Box key={index} horizontal align="center" style={{ margin: '.3rem 0' }}>
{props.action && (
<input type="checkbox"
onChange={handleCheckChieldElement}
checked={item.isChecked}
value={item.id}
style={{width: '20px', height: '15px'}} />
)}
{props.itemTemplate && props.itemTemplate(item) || (
<Text>{item.text}</Text>
)}
</Box>
))}
</Box>
</div>
</Box>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加
检查
属性到选择所有
输入而不是传递defaultchecked
属性。Add
checked
attribute toSelect All
input instead of passingdefaultChecked
attribute.