# antd table组件 编辑数据未更新问题

需求,有两个表格,点击第一个表格 取当前行的数据,再次点击第二个表格某项,完成数据匹配;

bug:数据其实是新数据,但是表单未更新,

解决方案:更新改变的某行的key

//ant table  表头;
  const designColumns = [
    {
      title: "编码",
      dataIndex: "FactoryMatCode",
      key: "FactoryMatCode",
      render: (text) => <a>{text}</a>,
    },
    {
      title: "类别",
      dataIndex: "MatType",
      key: "MatType",
    },

    {
      title: "物料名称",
      key: "MatName",
      dataIndex: "MatName",
    },
    {
      title: "规格型号",
      key: "MatSpec",
      dataIndex: "MatSpec",
    },
    {
      title: "备注",
      key: "Note",
      dataIndex: "Note",
    },
    {
      title: "工厂编码",     // ---需要更新的数据
      key: "MatCode",
      dataIndex: "MatCode",
    },
 
    {
      title: "匹配",
      key: "match",
      render: (_, record) => (
        <Button
          onClick={() => {
            beLink();      //赋值按钮, 
          }}
        >
          匹配
          {/* {toggle? '匹配':'取消匹配'} */}
        </Button>
      ),
      onChange: () => {
        console.log("first");
      },
    },
  ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 //拿到的后台数据;
let projectCode = sessionStorage.getItem("projectCode");
let str1 = `projectCode${projectCode}`; //数字签名拼接用
let signature1 = getSingature(timestamp, nonce, accountcode, SignToken, str1);
let param1 = { projectCode };
let headers1 = { accountcode, timestamp, nonce, signature: signature1 };

const designRes = http.get("/api/mes/GetProjectMatClassData", param1, headers1);
designRes.then((res) => {
    const data = res.data.Data;
    data.forEach((item) => {
        item.key = uuid();
    });
    setDesignArr(data);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 赋值按钮事件;

const beLink = () => {
    console.log(designArr, "dddd");
    setDesignArr([...designArr]);
    setToggle(!toggle);
};

1
2
3
4
5
6
7
8
//antd table 配置项; 

const rowSelection1 = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(selectedRows,'selectedRows')
      if (selectedRows[0]) {
        console.log(designArr, "hhhh");
        let designAr = [...designArr];
        selectedRows[0].key = uuid();     //关键代码----》数据添加完,重新赋值key
        selectedRows[0].MatCode = matCode;
        console.log((selectedRows[0].MatCode = matCode), "bbbbb");
        console.log(selectedRows[0], "aaaaaaa111");

        setDesignArr([...designAr]);
      }
    },
  };


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//antd table 
<Table
    rowSelection={{
        type: "checkbox",
            ...rowSelection1,
    }}
    className="list"
    columns={[...designColumns]}
    dataSource={[...designArr]}
    // dataSource={data1}
    pagination={false}
    />

1
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 8/20/2022, 16:01:00