2017-03-20 1 views
0

나는 작업자 이동 스케줄러를 구현하고 있습니다. 스크린 샷은 아래를 참조하십시오 :React Multi Column Select 체크 박스를 선택하십시오.

enter image description here

내가 백엔드에서 객체의 배열을받을 수 있습니다. ,

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":"00:00","shiftEnd":"08:00"},{"shiftStart":"08:00","shiftEnd":"15:00"},{"shiftStart":"15:00","shiftEnd":"00:00"}] 

이 내가 만든 반작용 구성 요소입니다 : 첫 번째는

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

두 번째 가능한 변화입니다 직원의 목록입니다 직원과 시프트가 선택된 상태를 어떻게 만들 수 있습니까? 그래서 나는 백엔드에 보낼 수있는 객체를 만들고 싶습니다.

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

더 구체적으로 말씀해주십시오. 백엔드에 이미 시프트 데이터와 직원 데이터가 있지만 React 구성 요소가 데이터의 "모양"을 변경하고 백엔드에 다시 제출하기를 원하십니까? 백엔드에서 데이터가 저장되는 방식을 제어 할 수 있습니까? – radiovisual

+0

당신은 각 직원에 대한 모든 체크 박스 값을 확인하는 것만으로 직원이 선택한 모든 근무 교대를받을 수 있습니다. –

답변

1

당신은 input 요소와 onChange 이벤트를 사용하고 직원의 ID와 해당 이벤트에 대한 각 이동 객체, 직원 목록을 저장하는 것 arraystate에서 변수를 정의 통과해야합니다.

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

확인 방법 state object에서 선택한 사용자를 저장하는 방법이 조각 (onChange 방법 당신이을해야 할 부분을 선택한 checkbox을 제거하는 추가 코드를 did't)

+0

감사합니다. 그러나 검사 상태에 대한 상태를 설정하는 데 문제가 있습니다. 지금은 하나의 체크 박스를 체크 할 때 체크 된 상태를 설정하는 방법을 모르기 때문에 체크 박스가 체크 된 상태로 표시되지 않습니다. –

관련 문제