首先,您应该添加
id
向每个用户发送。按用户的姓名识别用户是一种不好的做法:
constructor() {
super();
this.state = {
users: [
{
id: 1,
name: 'Jerry',
viewAccounts: true,
modifyAccounts: true,
viewUsers: false,
modifyUsers: true
},
{
id: 2,
name: 'George',
viewAccounts: false,
modifyAccounts: true,
viewUsers: false,
modifyUsers: false
},
{
id: 2,
name: 'Elaine',
viewAccounts: true,
modifyAccounts: false,
viewUsers: false,
modifyUsers: true
}
]
}
}
接下来,您应该提供
this.handleChange
作用
身份证件
用户的,
name
我们正在改变的财产和当前
value
:
renderRows(data) {
return data.map((step, index) =>
<tr key={index}>
<td>{step['name']}</td>
<td style={{align:"center", paddingLeft:"40px"}}>
<Input type="checkbox"
checked={step['viewAccounts']}
onChange={e => this.handleChange(step.id, 'viewAccounts', step['viewAccounts'])}/></td>
<td style={{align:"center", paddingLeft:"40px"}}>
<Input type="checkbox"
checked={step['modifyAccounts']}
onChange={e => this.handleChange(step.id, 'modifyAccounts', step['modifyAccounts'])}/></td>
<td style={{align:"center", paddingLeft:"40px"}}>
<Input type="checkbox"
checked={step['viewUsers']}
onChange={e => this.handleChange(step.id, 'viewUsers', step['viewUsers'])}/></td>
<td style={{align:"center", paddingLeft:"40px"}}>
<Input type="checkbox"
checked={step['modifyUsers']}
onChange={e => this.handleChange(step.id, 'modifyUsers', step['modifyUsers'])}/></td>
<td style={{align:"center"}}>
<Link to="/users"><i className="fa fa-edit fa-2x fa-fw" /></Link>
<Link to="/users"><i className="fa fa-times-circle fa-2x fa-fw" /></Link></td>
</tr>
);
}
this.handle更改
函数,我们应该根据给定的值更新特定的用户数据:
handleChange(id, name, value) {
this.setState({
users: this.state.users.map((user) => {
if (user.id !== id) return user;
// `Object.assign` function is used to return new modified object.
return Object.assign({}, user, {
// We should assign opposite to `value` variable value, as we are toggling permissions.
[name]: !value
});
});
});
}