我是一个初学者反应,我真的很难找出什么我做错了,我想我的填充数组位置检查复选框的值,以便能够POST有效载荷。作出反应 - 不能单独选择一个复选框选中后,他们都使用“全选”复选框
我的“位置”复选框,在我的DOM动态添加旁边的“全选”复选框。
当我点击我的位置复选框时,我可以在控制台上看到该复选框的值添加了positionChecked [] 但是小蓝色复选框从未触发。触发/取消触发的唯一方法是点击“全选”复选框。
位置复选框失去了他们的状态控制。
App.js
const items = [
'Position 1',
'Position 2',
'Position 3'
];
class AppG extends React.Component {
constructor(props) {
super(props);
this.state = {
positionChecked: [],
selectAll: false,
};
this.handleCheckboxSelection = this.handleCheckboxSelection.bind(this);
this.handleSelectAll = this.handleSelectAll.bind(this);
};
handleCheckboxSelection(e) {
e.preventDefault();
const newSelection = e.target.value;
let newSelectionArray;
if(this.state.positionChecked.indexOf(newSelection) > -1) {
newSelectionArray = this.state.positionChecked.filter(s => s !== newSelection)
} else {
newSelectionArray = [...this.state.positionChecked, newSelection];
}
this.setState({ positionChecked: newSelectionArray},() => console.log('position selection', this.state.positionChecked));
};
handleSelectAll(e) {
this.setState({selectAll:e.target.checked},() => console.log(this.state.selectAll));
};
handleSubmit = (e) => {
e.preventDefault();
const formPayload = {
positionChecked: this.state.positionChecked,
};
console.log('Send this in a POST request:', formPayload);
console.log(this.refs.checkbox.value);
};
createCheckboxes = (items) => (
items.map(this.createCheckbox)
);
createCheckbox = item => (
<Checkbox
inline
value={item}
onChange={this.handleCheckboxSelection}
checked={this.state.selectAll}
key={'position'}
type={'checkbox'}>
{item}
</Checkbox>
);
render() {
return (
<div>
<h1>Reporting by group</h1>
<Jumbotron>
<Grid>
<Row className="show-grid">
<Col sm={3}>
</Col>
<Col sm={6}>
<form onSubmit={this.handleSubmit} ref={form => this.form = form}>
<InputGroup />
<FormGroup style={styles}>
<Checkbox
inline
onChange={this.handleSelectAll}
checked={this.state.selectAll}
key={'All'}
value={'All'}
type={'checkbox'}>All
</Checkbox>
{this.createCheckboxes(items)}
</FormGroup>
<Datepicker />
<ButtonComponent type="submit" value="Submit"/>
</form>
</Col>
<Col sm={3}>
<NavComponent/>
</Col>
</Row>
</Grid>
</Jumbotron>
</div>
);
}
}
'检查= {this.state.selectAll}'是你的问题。 –