我正在创建这样的用户角色管理表单:
每当用户选中或取消选中权限时,我都会尝试将其置于状态,以便将其发送到后端服务器并更新数据库。
我将所有行信息都存储在componentWillReceiveProps
中的状态:
componentWillReceiveProps(nextProps){
if(nextProps.users.items){
this.setState({
userobj : nextProps.users.items['users']
});
const userarr = []
nextProps.users.items['users'].map((i) => {
userarr.push({"employeeid" : i['employeeid'] , "isadmin": i['isadmin'], "isreports" : i['isreports'], "ischarts": i['ischarts'], "ischathistory": i['ischathistory']})
});
this.setState({"list" : userarr});
}
}
现在状态为:
list: Object(4)
0: Object { employeeid: "12345", isadmin: false, isreports: true, … }
1: Object { employeeid: "12346", isadmin: false, isreports: true, … }
2: Object { employeeid: "12347", isadmin: false, isreports: true, … }
3: {…}
每个复选框都有一个onClick
事件:
<Table.Td>{rows['isadmin'] ? <Checkbox id={rows['employeeid']} name="isadmin" defaultChecked onChange={this.handleChange}></Checkbox> : <Checkbox id={rows['employeeid']} name="isadmin" onChange={this.handleChange}></Checkbox>}</Table.Td>
这是handleChange
的代码:
handleChange(id) {
const checked = id.target.checked;
const empid = id.target.id;
const name = id.target.name;
this.setState(prevState => ({
list: {
...prevState.list,
[this.getIndex(empid, this.state.list, 'employeeid')]: {
...prevState[this.getIndex(empid, this.state.list, 'employeeid')],
[name] : checked
},
}
}));
}
结果是:
list: Object(4)
0: Object { isadmin: true }
1: Object { employeeid: "12346", isadmin: false, isreports: true, … }
2: Object { employeeid: "12347", isadmin: false, isreports: true, … }
3: {…}
我需要实现的是:
list: Object(4)
0: Object { employeeid: "12345", isadmin: true, isreports: true, … }
1: Object { employeeid: "12346", isadmin: false, isreports: true, … }
2: Object { employeeid: "12347", isadmin: false, isreports: true, … }
3: {…}
我是React和Redux的新手。任何帮助表示赞赏。提前致谢。 ☺
答案 0 :(得分:0)
替换此:
ThisWorkbook.SaveAs Filename:=path, FileFormat:=xlCSV
与此:
this.setState(prevState => ({
list: {
...prevState.list,
[this.getIndex(empid, this.state.list, 'employeeid')]: {
...prevState[this.getIndex(empid, this.state.list, 'employeeid')],
[name] : checked
},
}
}));
答案 1 :(得分:0)
尝试将您的handleChange方法更新为此
handleChange(id) {
const checked = id.target.checked;
const empid = id.target.id;
const name = id.target.name;
this.setState(prevState => ({
let list = prevState.list
let user = prevState.list.filter(user => user.employeeid == id)
const userIndex = prevState.list.indexOf(user)
user = {
...user,
isadmin: true
}
list.splice(userIndex, 1) //remove the old user
list.splice(userIndex, 0, user) //put the updated user at same index
this.setState({list})
}));
}