Reactjs 基于索引选中并按1选择1后如何更改复选框
我有数据,我需要在复选框中选择它,但在我的函数onChange上,当我选择该框时,所有列表都被选中,如何根据它们的索引逐个选择项目1 我不知道,如何选择他们的索引,有人能帮我吗Reactjs 基于索引选中并按1选择1后如何更改复选框,reactjs,Reactjs,我有数据,我需要在复选框中选择它,但在我的函数onChange上,当我选择该框时,所有列表都被选中,如何根据它们的索引逐个选择项目1 我不知道,如何选择他们的索引,有人能帮我吗 const data = [ { item : 'HKK50_BBJ', qty : 2, sell : '28754', buy : '-', avail : 2, date :
const data = [
{
item : 'HKK50_BBJ',
qty : 2,
sell : '28754',
buy : '-',
avail : 2,
date : '2019-07-08'
}
];
for(var i = 0; i < 15; i++) {
data.push(data[0]);
};
class OpenPositions extends Component {
constructor(props) {
super(props);
this.state = {
newArr : [],
checked: false,
};
}
handleCheckBox(index){
console.log(index);
this.setState({
checked : !this.state.checked
})
}
render() {
return (
<div style={{height:'100%', overflow:'auto'}}>
<table className="borderline" cellspacing="0" cellpadding="0" border="0" style={{height:'100%'}}>
<tbody>
<tr>
<td>
<div>
<div>
<table style={{height:"100%", width:"100%"}}>
<tbody>
<tr align="center" className="fieldtitle fieldbg">
<td align="left">Items</td>
<td>Qty</td>
<td>Sold</td>
<td>Bought</td>
<td>Avail</td>
<td>Date</td>
<td>Options</td>
</tr>
</tbody>
<tbody className="divOP2">
{data.map((item, index) =>{
console.log(item, "ini item bois");
console.log(index, "ini index")
return(
<tr align="center" className="contentbg">
<td align="left" nowrap className="bggradientleft"><font color="#FFFFFF"><strong>{item.product}</strong></font></td>
<td>{item.qty}</td>
<td>{item.sell}</td>
<td>{item.buy}</td>
<td>{item.avail}</td>
<td>{item.date}</td>
<td>
<input
className="checkbox"
type="checkbox"
checked={this.state.checked}
onChange={this.handleCheckBox.bind(this, index)}
/>
</td>
</tr>
)})}
</tbody>
</table>
</div>
</div>
</td>
</tr>
<table className="normal" cellspacing="0" cellpadding="0" border="0" style={{height:'100%'}}>
<tbody>
<tr>
<td>
<button type="submit" className="normalButton wide">GTC Order (Liq)</button>
</td>
<td>
<button type="submit" className="normalButton wide" onClick={this.handleBuySellLiq.bind(this)}>Buy/Sell (Liq)</button>
</td>
</tr>
</tbody>
</table>
</tbody>
</table>
</div>
)
}
};
const数据=[
{
项目:'HKK50_BBJ',
数量:2,
出售:'28754',
买:'-',,
效用:2,
日期:2019-07-08
}
];
对于(变量i=0;i<15;i++){
data.push(数据[0]);
};
类OpenPositions扩展组件{
建造师(道具){
超级(道具);
此.state={
纽瓦尔:[],
勾选:假,
};
}
handleCheckBox(索引){
控制台日志(索引);
这是我的国家({
选中:!this.state.checked
})
}
render(){
返回(
项目
数量
出售
买了
效用
日期
选择权
{data.map((项目,索引)=>{
控制台日志(项目“ini项目bois”);
console.log(索引,“ini索引”)
返回(
{item.product}
{项目数量}
{item.sell}
{item.buy}
{item.avail}
{item.date}
)})}
GTC订单(液体)
买入/卖出(液体)
)
}
};
我希望通过索引逐个选中复选框,但我不知道如何将选中的属性添加到
数据中的每个对象:
{
item : 'HKK50_BBJ',
qty : 2,
sell : '28754',
buy : '-',
avail : 2,
date : '2019-07-08',
checked : false,
}
然后:
handleCheckBox(index) {
console.log(index);
const itemToChange = this.state.newArr[index];
itemToChange.checked = !itemToChange.checked;
const newArray = [...this.state.newArr];
newArray[index] = itemToChange;
this.setState({
newArr: newArray,
})
}
更新:
这是给你的裁判的一封信
将选中的特性添加到数据中的每个对象中
:
{
item : 'HKK50_BBJ',
qty : 2,
sell : '28754',
buy : '-',
avail : 2,
date : '2019-07-08',
checked : false,
}
然后:
handleCheckBox(index) {
console.log(index);
const itemToChange = this.state.newArr[index];
itemToChange.checked = !itemToChange.checked;
const newArray = [...this.state.newArr];
newArray[index] = itemToChange;
this.setState({
newArr: newArray,
})
}
更新:
这是给你的裁判的一封信