Javascript 如何处理复选框数组的状态?
有没有办法处理复选框数组的选中状态 我有这个阵列:Javascript 如何处理复选框数组的状态?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,有没有办法处理复选框数组的选中状态 我有这个阵列: const CheckboxItems = t => [ { checked: true, value: 'itemsCancelled', id: 'checkBoxItemsCancelled', labelText: t('cancellations.checkBoxItemsCancelled'), }, { checked: true, value: 'requestD
const CheckboxItems = t => [
{
checked: true,
value: 'itemsCancelled',
id: 'checkBoxItemsCancelled',
labelText: t('cancellations.checkBoxItemsCancelled'),
},
{
checked: true,
value: 'requestDate',
id: 'checkboxRequestDate',
labelText: t('cancellations.checkboxRequestDate'),
},
{
checked: true,
value: 'status',
id: 'checkboxStatus',
labelText: t('cancellations.checkboxStatus'),
},
{
checked: true,
value: 'requestedBy',
id: 'checkboxRequestedBy',
labelText: t('cancellations.checkboxRequestedBy'),
},
];
我在这里使用它:
class TableToolbarComp extends React.Component {
state = {
isChecked: true,
};
onChange = (value, id, event) => {
this.setState(({ isChecked }) => ({ isChecked: !isChecked }));
};
render() {
const { isChecked } = this.state;
return (
{CheckboxItems(t).map(item => (
<ToolbarOption key={item.id}>
<Checkbox
id={item.id}
labelText={item.labelText}
value={item.value}
checked={isChecked}
onChange={this.onChange}
/>
</ToolbarOption>
))}
)
}
}
类tabletorbacomp扩展了React.Component{
状态={
我问:是的,
};
onChange=(值、id、事件)=>{
this.setState(({isChecked})=>({isChecked:!isChecked}));
};
render(){
const{isChecked}=this.state;
返回(
{CheckboxItems(t).map(item=>(
))}
)
}
}
我遇到的问题是,每次我不检查一个,其余的也不检查。我需要单独管理状态,通过redux操作将一些信息发送给其他组件
编辑:
您正在使用容器的isChecked
作为所有复选框的状态,使用容器上的一个方法翻转它应用于所有复选框的一个标志(isChecked
)
取而代之的是:
class TableToolbarComp extends React.Component {
state = {
items: CheckboxItems(t) // Your code seems to have a global called `t`
};
onChange = (value, id, event) => {
this.setState(({ items }) => {
// Copy the array
items = items.slice();
// Find the matching item
const item = items.find(i => i.id === id);
if (item) {
// Update its flag and set state
item.checked = !item.checked;
return { items };
}
});
};
render() {
const { items } = this.state;
return (
{items.map(item => (
<ToolbarOption key={item.id}>
<Checkbox
id={item.id}
labelText={item.labelText}
value={item.value}
checked={item.checked}
onChange={this.onChange}
/>
</ToolbarOption>
))}
)
}
}
类tabletorbacomp扩展了React.Component{
状态={
items:CheckboxItems(t)//您的代码似乎有一个名为't'的全局`
};
onChange=(值、id、事件)=>{
this.setState(({items})=>{
//复制数组
items=items.slice();
//找到匹配项
const item=items.find(i=>i.id==id);
如果(项目){
//更新其标志并设置状态
item.checked=!item.checked;
返回{items};
}
});
};
render(){
const{items}=this.state;
返回(
{items.map(item=>(
))}
)
}
}
变化:
- 调用
一次,将结果保留为状态CheckboxItems
- 在
中,通过onChange
(lib传递id
)找到相关复选框,并翻转其id
标志选中的
- 在
中,从状态中获取render
,对于每个项,使用其项
标志,而不是您的`isChecked(我已完全删除)已选中的
isChecked
作为所有复选框的状态,使用容器上的方法翻转应用于所有复选框的一个标志(isChecked
)
取而代之的是:
class TableToolbarComp extends React.Component {
state = {
items: CheckboxItems(t) // Your code seems to have a global called `t`
};
onChange = (value, id, event) => {
this.setState(({ items }) => {
// Copy the array
items = items.slice();
// Find the matching item
const item = items.find(i => i.id === id);
if (item) {
// Update its flag and set state
item.checked = !item.checked;
return { items };
}
});
};
render() {
const { items } = this.state;
return (
{items.map(item => (
<ToolbarOption key={item.id}>
<Checkbox
id={item.id}
labelText={item.labelText}
value={item.value}
checked={item.checked}
onChange={this.onChange}
/>
</ToolbarOption>
))}
)
}
}
类tabletorbacomp扩展了React.Component{
状态={
items:CheckboxItems(t)//您的代码似乎有一个名为't'的全局`
};
onChange=(值、id、事件)=>{
this.setState(({items})=>{
//复制数组
items=items.slice();
//找到匹配项
const item=items.find(i=>i.id==id);
如果(项目){
//更新其标志并设置状态
item.checked=!item.checked;
返回{items};
}
});
};
render(){
const{items}=this.state;
返回(
{items.map(item=>(
))}
)
}
}
变化:
- 调用
一次,将结果保留为状态CheckboxItems
- 在
中,通过onChange
(lib传递id
)找到相关复选框,并翻转其id
标志选中的
- 在
中,从状态中获取render
,对于每个项,使用其项
标志,而不是您的`isChecked(我已完全删除)已选中的