我是一個初學者反應,我真的很難找出什麼我做錯了,我想我的填充數組位置檢查複選框的值,以便能夠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}'是你的問題。 –