react+sntdesign+Table问题解决----删除某列数据,错删别的数据。数据源正确,但是展示错误。

本文探讨了一位开发者在React中遇到的数据更新问题,页面显示错误但数据处理正确。通过尝试深复制、避免使用filter和splice,问题仍未解决。最终,通过为元素添加key属性,问题得到解决。文章提到了React组件渲染的关键在于key,以及如何在自定义渲染函数中正确使用key。
摘要由CSDN通过智能技术生成

一开始是 2 3 4,
然后我删除2之后,
页面展示是错,但是数据是对的。
请添加图片描述请添加图片描述
代码如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
问题分析:
1.排除浅复制的问题,换成了深复制
2.排除了filter可能导致的问题。换成了splice
3.----1.2之后依然不行。
最后看官网发现有个key。尝试了下,可以了。

解决办法:

key = {record.XX}

在这里插入图片描述

 const rgRulesColums2: any = [
        {
            title: '染料(编号)',
            dataIndex: 'stuffNum',
            align: 'center',
            editable: true,
            render: (text: any, record: any, index: number) => {
                return (
                    <Input
                        bordered={false}
                        defaultValue={record.stuffNum}
                        key={record.stuffNum}
                        style={{ textAlign: 'center' }}
                        onChange={(e) => {
                            noStuffNumChange(e, text, record, index);
                        }}
                    />
                );
            },
        },
        {
            title: '燃料分类',
            dataIndex: 'type',
            align: 'center',
            editable: true,
            render: (text: any, record: any, index: number) => {
                //参数分别为当前行的值,当前行数据,行索引
                return (
                    <Select
                        onChange={(val: any) => {
                            noStuffTypeChange(val, index);
                        }}
                        key={record.type} 
                        defaultValue={record.type}
                        style={{ width: 120 }} >
                        <Option value="普通">{'普通'}</Option>
                        <Option value="特殊">{'特殊'} </Option>
                   </Select>
                );
            },
        },
        {
            title: '删除',
            align: 'center',
            dataIndex: 'operation',
            width: 60,
            render: (text: any, record: any, index: number) =>
                rgDataSource2.length >= 1 ? (
                    <Popconfirm title="确定删除吗?" onConfirm={() => andDelete(index)}>
                        <a>删除</a>
                    </Popconfirm>
                ) : null,
        },
    ];

当然你直接使用官方demo的那种也可以,这样就不需要自己手动添加再元素上面的key了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值