Browse Source

fix: DIYTable onChange 事件不刷新问题

ZhaoJun 1 year ago
parent
commit
8ba2b96960
1 changed files with 14 additions and 14 deletions
  1. 14 14
      src/components/DDComponents/DIYTable/index.tsx

+ 14 - 14
src/components/DDComponents/DIYTable/index.tsx

@@ -1,7 +1,7 @@
 import { PlusOutlined } from '@ant-design/icons';
 import { Button, Input, Table } from 'antd';
 import { ColumnsType } from 'antd/lib/table';
-import React, { useEffect, useState } from 'react';
+import React, { useState } from 'react';
 import DDDateField from '@/components/DDComponents/DDDateField';
 import DDSelectField from '@/components/DDComponents/DDSelectField';
 import NumberField from '@/components/DDComponents/NumberField';
@@ -30,13 +30,17 @@ function DIYTable(props: IProps) {
   // table数据
   const [tableData, setTableData] = useState<TableDataType[]>([{ index: 1 }]);
   // table 列配置
-  const [tableColumnDef, setTableColumnDef] = useState<ColumnsType<any>>([
+  // const [tableColumnDef, setTableColumnDef] = useState<ColumnsType<any>>([
+  //
+  // ]);
+
+  const tableColumnDef: ColumnsType<any> = [
     {
       title: '序号',
       dataIndex: 'index',
       className: 'hidden',
     },
-  ]);
+  ];
 
   // 表单填写时的表格生成
   const handleGenerateTable = () => {
@@ -140,7 +144,6 @@ function DIYTable(props: IProps) {
   const handleDisplayOnly = () => {
     const rows = columns;
     const newTableData = [];
-    const newColumnDef: ColumnsType<any> = [];
     if (rows && columns.length) {
       for (let index = 0; index < rows.length; index++) {
         // 把每一行的数据提出来到一个对象里
@@ -150,7 +153,7 @@ function DIYTable(props: IProps) {
           if (index === 0) {
             // 列配置
             row.forEach((col: any) => {
-              newColumnDef.push({ title: col.name, dataIndex: col.type });
+              tableColumnDef.push({ title: col.name, dataIndex: col.type });
             });
           }
           const rowData: any = {};
@@ -158,10 +161,9 @@ function DIYTable(props: IProps) {
             rowData[col.type] = col.value[0];
             rowData.key = col.id + index;
           });
-          newTableData.push(rowData);
+          tableColumnDef.push(rowData);
         }
       }
-      setTableColumnDef(newColumnDef);
       setTableData(newTableData);
     }
   };
@@ -170,13 +172,11 @@ function DIYTable(props: IProps) {
     setTableData([...tableData, { index: tableData.length + 1 }]);
   };
 
-  useEffect(() => {
-    if (displayOnly) {
-      handleDisplayOnly();
-    } else {
-      handleGenerateTable();
-    }
-  }, []);
+  if (displayOnly) {
+    handleDisplayOnly();
+  } else {
+    handleGenerateTable();
+  }
 
   return (
     <>