我想要实现一个对数组的删除功能 是根据存在state里面的数组的id值删除的 但是删除了以后 其他的记录的id值不会改变 所以就导致再点击删除别的记录的时候 会删除的不是我点击的那一条 还是个react小白 想不出解决办法了 求助大神们 谢谢啦 代码附上
this.state={
visible:false,
showChangeVisible :false,
list:[
{
id:'0',
subname:'语文',
teacher:'Wang',
hours:'24'
},
{
id:'1',
subname:'数学',
teacher:'Wang',
hours:'24'
},
{
id:'2',
subname:'英语',
teacher:'Wang',
hours:'24'
},
{
id:'3',
subname:'物理',
teacher:'Wang',
hours:'24'
},
{
id:'4',
subname:'化学',
teacher:'Wang',
hours:'24'
},
{
id:'5',
subname:'生物',
teacher:'Wang',
hours:'24'
},
{
id:'6',
subname:'政治',
teacher:'Wang',
hours:'24'
},
{
id:'7',
subname:'历史',
teacher:'Wang',
hours:'24'
},
{
id:'8',
subname:'地理',
teacher:'Wang',
hours:'24'
},
{
id:'9',
subname:'计算机',
teacher:'Wang',
hours:'24'
},],
total:null,
};
删除方法如下
// 删除
handleDelete = (id) => {
const list = this.state.list;
debugger
list.splice(id, 1);
this.setState ({
list : list,
})
};
然后点击按钮删除我是这么写的,用的antd 的Table组件来显示数据
const columns=[{
title:'课程题目',
dataIndex:'subname',
id:'subname',
},
{
title:'任课教师',
dataIndex:'teacher',
key:'teacher',
},
{
title:'总课时',
dataIndex:'hours',
key:'hours',
},
{
title:'操作',
key:'action',
render:(text,record)=>(
<span>
<Button onClick={this.showChangeModal.bind(null,record)} type="primary">修改</Button>
<ChangeForm
visible={this.state.showChangeVisible}
onCancel={this.handleChangeCancel}
onChangeItem={this.handleChange}
record={record}
/>
<Divider type="vertical"/>
<Button onClick={this.handleDelete.bind(null,record.id)} type="primary">删除</Button>
</span>
)
},];