2017-03-20 65 views
0

我正在實施一個工人班次調度程序。見下面的截圖:React多列選擇複選框

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"}] 

這是我創建的陣營組成:

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" 
}] 
+0

你能更具體嗎?您已經在後端擁有班次數據和員工數據,但是您希望您的React組件更改數據的「形狀」並重新提交到後端?你是否可以控制數據如何保存在後端? – radiovisual

+0

你只需要檢查每個員工的所有複選框的值,這樣你會得到所有員工選擇的班次 –

回答

1

需要使用onChange事件與input元素,並通過員工ID和每個班次對象該事件,定義arraystate變量將存儲在員工列表。

檢查這個片段,如何選定的用戶存儲在state object我以前不添加代碼在onChange方法刪除選定checkbox,你需要做的那部分):

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'/>

+0

感謝您的支持。但是,我無法爲檢查狀態設置狀態。現在,當您選中一個複選框時,它不會顯示覆選框,因爲我不知道如何設置檢查狀態。 –