Преглед изворни кода

fix: 修复不能读取tableData的问题

ZhaoJun пре 2 година
родитељ
комит
50fbf445cb
1 измењених фајлова са 66 додато и 58 уклоњено
  1. 66 58
      src/pages/Flow/OaDetail.js

+ 66 - 58
src/pages/Flow/OaDetail.js

@@ -97,18 +97,6 @@ const OaDetail = () => {
     let [columnID, tableID] = ids[1].split('>');
     let [columnLabel, tableLabel] = label.split('>');
 
-    const { columns: originColumns } = data.formData.find(
-      (item) => item.props.id === tableID,
-    );
-
-    // 检查是否有文字说明控件,有的话需要写入tableData
-    const allTextNote = originColumns.filter((item, index) => {
-      if (item.componentName === 'TextNote') {
-        item.index = index;
-        return true;
-      }
-    });
-
     const cell = {
       name: columnLabel,
       id: columnID,
@@ -118,27 +106,19 @@ const OaDetail = () => {
     // 组装可能用到的数据
     const cols = [];
     cols[colIndex] = cell;
-
-    // 对这个表格的每个第 allTextNote item 的 index 列写入
-    let textNoteCellList = [];
-    if (allTextNote && allTextNote.length) {
-      // 不可编辑控件
-      textNoteCellList = allTextNote.map((item) => {
-        return {
-          name: item.props.label,
-          id: item.props.id,
-          type: item.componentName,
-          value: [item.props.placeholder],
-          colIndex: item.index,
-        };
-      });
-    }
-
     const rows = [];
     rows[rowIndex] = cols;
-    // 可能不止一个表格
-    if (tableData.length > 0) {
-      let table = tableData.find((item) => item.id === tableID);
+    // 如果已经有数据
+    let oldTableData = [];
+    // 这里不知道为什么不能直接读取state(tableData)
+    setTableData((prevState) => {
+      oldTableData = prevState;
+      return prevState;
+    });
+    console.clear();
+    console.log(oldTableData);
+    if (oldTableData && oldTableData.length > 0) {
+      let table = oldTableData.find((item) => item.id === tableID);
       // 如果某个表格数据存在
       if (table) {
         const oldRows = table.value;
@@ -150,12 +130,11 @@ const OaDetail = () => {
             // 记录可编辑控件
             table.value[rowIndex][colIndex] = cell;
             // 不可编辑控件
-            if (textNoteCellList.length) {
-              for (const note of textNoteCellList) {
-                table.value[rowIndex][note.colIndex] = note;
-              }
-            }
-            const newTableData = tableData.map((item) => {
+            table.value[rowIndex] = addUnEditableColumn(
+              table.value[rowIndex],
+              tableID,
+            );
+            const newTableData = oldTableData.map((item) => {
               if (item.id === table.id) {
                 return table;
               }
@@ -168,17 +147,17 @@ const OaDetail = () => {
             // 写入可编辑控件
             table.value[rowIndex] = cols;
             // 写入不可编辑控件的值
-            if (textNoteCellList.length) {
-              for (const note of textNoteCellList) {
-                table.value[rowIndex][note.colIndex] = note;
-              }
-            }
-            const newTableData = tableData.map((item) => {
+            table.value[rowIndex] = addUnEditableColumn(
+              table.value[rowIndex],
+              tableID,
+            );
+            const newTableData = oldTableData.map((item) => {
               if (item.id === table.id) {
                 return table;
               }
               return item;
             });
+            setTableData([]);
             setTableData(newTableData);
           }
         } else {
@@ -186,12 +165,11 @@ const OaDetail = () => {
           // 写入可编辑控件
           table.value = rows;
           // 写入不可编辑控件
-          if (textNoteCellList.length) {
-            for (const note of textNoteCellList) {
-              table.value[rowIndex][note.colIndex] = note;
-            }
-          }
-          const newTableData = tableData.map((item) => {
+          table.value[rowIndex] = addUnEditableColumn(
+            table.value[rowIndex],
+            tableID,
+          );
+          const newTableData = oldTableData.map((item) => {
             if (item.id === table.id) {
               return table;
             }
@@ -200,8 +178,9 @@ const OaDetail = () => {
           setTableData(newTableData);
         }
       } else {
+        // 如果某个table的数据不存在
+        rows[rowIndex] = addUnEditableColumn(rows[rowIndex], tableID);
         const newTableData = [
-          ...tableData,
           {
             name: tableLabel,
             id: tableID,
@@ -209,14 +188,12 @@ const OaDetail = () => {
             value: rows,
           },
         ];
-        setTableData(newTableData);
+        setTableData([...oldTableData, ...newTableData]);
       }
     } else {
-      if (textNoteCellList.length) {
-        for (const note of textNoteCellList) {
-          rows[rowIndex][note.colIndex] = note;
-        }
-      }
+      // 如果没有数据
+      // 添加不可编辑控件
+      rows[rowIndex] = addUnEditableColumn(rows[rowIndex], tableID);
       const newTableData = [
         {
           name: tableLabel,
@@ -225,11 +202,43 @@ const OaDetail = () => {
           value: rows,
         },
       ];
-      console.log(newTableData);
       setTableData(newTableData);
     }
   };
 
+  const addUnEditableColumn = (rows, tableID) => {
+    const { columns: originColumns } = data.formData.find(
+      (item) => item.props.id === tableID,
+    );
+    // 检查是否有文字说明控件,有的话需要添加在第几列的index后写入tableData
+    const allTextNote = originColumns.filter((item, index) => {
+      if (item.componentName === 'TextNote') {
+        item.index = index;
+        return true;
+      }
+      return false;
+    });
+    // 对这个表格的每个第 (allTextNote.item 的 index )列写入
+    let textNoteCellList = [];
+    if (allTextNote && allTextNote.length) {
+      textNoteCellList = allTextNote.map((item) => {
+        return {
+          name: item.props.label,
+          id: item.props.id,
+          type: item.componentName,
+          value: [item.props.placeholder],
+          colIndex: item.index,
+        };
+      });
+    }
+    if (textNoteCellList.length) {
+      for (const note of textNoteCellList) {
+        rows[note.colIndex] = note;
+      }
+    }
+    return rows;
+  };
+
   const submit = async () => {
     form.validateFields().then((values) => {
       const { form: formCur } = formValueRef.current;
@@ -264,7 +273,6 @@ const OaDetail = () => {
       tableIndex.forEach((item, index) => {
         formData.splice(item, 0, tableData[index]);
       });
-
       createRun({
         flow_id: Number(oaId),
         form: JSON.stringify(formData),