简体   繁体   English

单击签入反应时,将行数据存储为处于状态的对象数组

[英]Store row data as array of object in state when click on checked in react

<table className="table sm-td table-bordered mt-2">
          <tbody>
            {step1response.skills.length? (
              step1response.skills.slice(0, 5).map((t) => (
                <tr key={t}>
                  <td>
                    <input type="checkbox" onChange={handleSkills}/>
                  </td>
                  <td>{t}</td>
                  <td>
                    <select name="skill_type" className="custom-input p1" onChange={handleChange}>
                      <option>Select</option>
                      <option value="Must Have" selected>Must have</option>
                      <option value="Nice to have">Nice to have</option>
                    </select>
                  </td>
                  <td>
                    <select name="level_type" className="custom-input p1" onChange={handleChange}>
                      <option>Select</option>
                      <option value="Distinguished level" selected>Distinguished Level</option>
                      <option value="Novice level">Novice Level</option>
                    </select>
                  </td>
                </tr>

So i am mapping through an array which have skills names in it...i want the user to select the level and skill type in the other two select dropdowns in the table row.所以我正在映射一个包含技能名称的数组......我希望用户在表格行的其他两个选择下拉列表中选择级别和技能类型。 I am not able to figure out how can i store the selected row data when user clicks on that checkbox and gather the values whatever he has selected so my state can look like:我无法弄清楚当用户单击该复选框并收集他选择的任何值时如何存储所选行数据,以便我的状态看起来像:

const [state,setState] = useState([{
name:"",
skill_type:"",
level_type:""
}])

you need to add properties to each object of skills on drop down selection like this :您需要在下拉选择中为每个技能对象添加属性,如下所示:

handleChangeSkillType =(e)=>{
   skills[index].skill_type=e.target.value
}
handleChangeLevelType =(e)=>{
   skills[index].level_type=e.target.value
}
handleSkills=(e,index)=>{

   // you will be able to access slected row value here by skills[index]

}

And while calling onChange pass index as parameter for all like :并且在调用 onChange 传递索引作为参数时例如:

onChange={(e)=>handleChangeLevelType(e,index)}
onChange={(e)=>handleSkills(e,index)}
onChange={(e)=> handleChangeLevelType(e,index)}

and pass index to map function并将索引传递给映射函数

step1response.skills.slice(0, 5).map((t,index) => 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM