gpt4 book ai didi

javascript - react 多列选择复选框

转载 作者:行者123 更新时间:2023-11-29 21:08:20 25 4
gpt4 key购买 nike

我正在实现 worker 轮类调度程序。请参见下面的屏幕截图:

enter image description here

我从后端接收到一个对象数组。第一个是员工列表

[{
"employeeId": "id",
"name": "bob"
},{
"employeeId": "id",
"name": "steve",
}]

第二个是可用类次:

[{"shiftStart":"00:00","shiftEnd":"08:00"},{"shiftStart":"08:00","shiftEnd":"15:00"},{"shiftStart":"15:00","shiftEnd":"00:00"}]

这是我创建的 React 组件:

    class PlanningShiftManagement extends Component {
render() {
const availableShifts = this.props.shifts.map((shift) => {
return (
<th>
{shift.shiftStart} - {shift.shiftEnd}
</th>
)
})

const employees = this.props.employeeList.map((employee) => {
let employeecheckbox = this.props.shifts.map((shift) => {
return(
<td>
<input type="checkbox" />
</td>
)
});
return(
<tr className="table-row">
<td>{employee.title}</td>
{employeecheckbox}
</tr>
)
})
return(
<div>
<table className="scheduler-table">
<thead className="table-head">
<tr>
<th>Employee Name</th>
{availableShifts}
</tr>
</thead>
<tbody className="table-body">
{employees}
</tbody>
</table>
</div>
);
}
}

现在,我的问题是,如何创建包含员工和所选类次的状态。所以我想创建一个可以发送到后端的对象。

 [{
"shiftStart": "20170313 10:00",
"shiftEnd": "20170313 17:00",
"employeeId": "id"
}]

最佳答案

您需要将 onChange 事件与 input 元素一起使用,并将员工 ID 和每个类次对象传递给该事件,定义一个 array state 将存储员工列表的变量。

检查此代码段,如何将选定的用户存储在状态对象中(没有添加代码来删除选定的复选框onChange 方法中,你需要做的部分):

let employee = [{
"employeeId": "1",
"name": "bob"
},{
"employeeId": "2",
"name": "steve",
}];

let shift = [{"shiftStart":"00:00","shiftEnd":"08:00"},{"shiftStart":"08:00","shiftEnd":"15:00"},{"shiftStart":"15:00","shiftEnd":"00:00"}];

class PlanningShiftManagement extends React.Component {
constructor(){
super();
this.state = {users: []}
}

_populateTableData(){
return this.props.employeeList.map((employee) => {
let employeecheckbox = this.props.shifts.map((shift) => {
return(
<td>
<input type="checkbox" onChange={this._inputChange.bind(this, shift, employee.employeeId)}/>
</td>
)
});
return(
<tr className="table-row">
<td>{employee.name}</td>
{employeecheckbox}
</tr>
)
})
}

_inputChange(shift, employeeId){
let users = JSON.parse(JSON.stringify(this.state.users));
users.push({
start: shift.shiftStart,
end: shift.shiftEnd,
id: employeeId
});
this.setState({users});
console.log(users);
}

render() {
const availableShifts = this.props.shifts.map((shift) => {
return (
<th>
{shift.shiftStart} - {shift.shiftEnd}
</th>
)
})

return(
<div>
<table className="scheduler-table">
<thead className="table-head">
<tr>
<th>Employee Name</th>
{availableShifts}
</tr>
</thead>
<tbody className="table-body">
{this._populateTableData()}
</tbody>
</table>
</div>
);
}
}

ReactDOM.render(<PlanningShiftManagement employeeList = {employee} shifts={shift}/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'/>

关于javascript - react 多列选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42897068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com