tangjunhao 2 bulan lalu
induk
melakukan
ff1607fcd8
1 mengubah file dengan 81 tambahan dan 52 penghapusan
  1. 81 52
      src/views/model/vueflow/aside/asideData.vue

+ 81 - 52
src/views/model/vueflow/aside/asideData.vue

@@ -470,6 +470,7 @@ const getbtnvalue = async (pcaId, dataType) => {
         if (!item?.pcadgId) return
         
         const rowId = item.pcadgId
+        // 为行加pcadgId
         if (!rowMap.has(rowId)) {
           rowMap.set(rowId, { pcadgId: rowId })
         }
@@ -508,8 +509,10 @@ const getbtnvalue = async (pcaId, dataType) => {
       }))
 
       // 确保所有数据处理完成后再设置 tableData
-      tableData.value = Array.from(rowMap.values())
-      console.log("tableData.value", tableData.value)
+      // tableData.value = Array.from(rowMap.values())
+      // 排序
+      tableData.value = Array.from(rowMap.values()).sort((a, b) => a.pcadgId - b.pcadgId)
+      console.log("tableData赋值:",tableData.value)
 
     } else if (dataType === 2) {
       // 处理属性-值对形式
@@ -645,15 +648,13 @@ const currentRow = ref(null) // 当前选中行
 
 // 创建新行的辅助函数
 const createNewRow = () => {
-  const rowIndex = tableData.value.length + 1
   const newRow = {}
-  console.log('tableColumns.value',tableColumns.value)
   tableColumns.value.forEach((col) => {
     newRow[col.code] = {
       value: col.valueType === 1 ? col.options?.[0]?.val || "" : "",
       unit: col.unit || "无",
       options: col.valueType === 1 ? col.options : [],
-      pcadgId: rowIndex,
+      pcadgId: "", // 初始化为空字符串
       pcadId: "",
       cdvId: col.cdvId
     }
@@ -678,46 +679,69 @@ const handleRowClick = (row) => {
   tableRef.value?.setCurrentRow(row)
 }
 
-// 删除选中行
-const handleDeleteSelected = () => {
+// 删除选中行 - 如果pcadgId不为空则调用API删除
+const handleDeleteSelected = async () => {
   if (!currentRow.value) {
     ElMessage.warning("请先点击选择要删除的行")
     return
   }
 
   const index = tableData.value.findIndex((row) => row === currentRow.value)
-  if (index !== -1) {
-    tableData.value.splice(index, 1)
+  if (index === -1) return
 
-    // 更新后续行的 pcadgId
-    for (let i = index; i < tableData.value.length; i++) {
-      const newPcadgId = i + 1 // 行号从1开始
-
-      // 更新行中每个列的 pcadgId
-      tableColumns.value.forEach((col) => {
-        if (
-          tableData.value[i][col.code] &&
-          typeof tableData.value[i][col.code] === "object"
-        ) {
-          tableData.value[i][col.code].pcadgId = newPcadgId
-        }
-      })
-    }
+  // 检查是否有pcadgId(非空字符串)
+  const pcadgId = currentRow.value.pcadgId
+  
+  console.log("获取到的pcadgId:", pcadgId) // 调试用
 
-    // 删除后自动选中上一行或下一行(如果还有行)
-    if (tableData.value.length > 0) {
-      const newIndex = Math.min(index, tableData.value.length - 1)
-      currentRow.value = tableData.value[newIndex]
-      nextTick(() => {
-        tableRef.value?.setCurrentRow(currentRow.value)
-      })
-    } else {
-      currentRow.value = null
+  try {
+    if (pcadgId && pcadgId !== "") {
+      // 使用await等待删除完成
+      await deleterow(pcadgId)
     }
+    
+    // 无论是否有pcadgId,最终都要从本地删除
+    tableData.value.splice(index, 1)
+    updateSelectionAfterDelete(index)
+    
+  } catch (error) {
+    ElMessage.error("删除失败,请重试")
+    console.error("删除过程中出错:", error)
+  }
+}
+
+// 删除后更新选中行的辅助函数
+const updateSelectionAfterDelete = (deletedIndex) => {
+  if (tableData.value.length > 0) {
+    const newIndex = Math.min(deletedIndex, tableData.value.length - 1)
+    currentRow.value = tableData.value[newIndex]
+    nextTick(() => {
+      tableRef.value?.setCurrentRow(currentRow.value)
+    })
+  } else {
+    currentRow.value = null
+  }
+  ElMessage.success("删除成功")
+}
+
+// 后端删除API调用
+const deleterow = async (pcadgId) => {
+  const params = {
+    transCode: "ES0016",
+    pcaId: pcaId,
+    pcadgId: pcadgId
+  }
+  
+  try {
+    const res = await request(params)
+    return res // 返回成功结果
+  } catch (err) {
+    console.error("删除请求失败:", err)
+    throw err // 重新抛出错误以便外部捕获
   }
 }
 
-// 在选中行下方插入新行
+// 在选中行下方插入新行 - 简化版,不再更新pcadgId
 const handleInsertSelected = () => {
   if (!currentRow.value && tableData.value.length > 0) {
     ElMessage.warning("请先点击选择要插入的位置")
@@ -728,24 +752,9 @@ const handleInsertSelected = () => {
     ? tableData.value.findIndex((row) => row === currentRow.value)
     : -1
 
-  // 创建新行时使用正确的行号(插入位置+1)
   const newRow = createNewRow()
-
-  // 插入新行
   tableData.value.splice(index + 1, 0, newRow)
 
-  // 更新所有后续行的行号,包括该行
-  for (let i = index + 1; i < tableData.value.length; i++) {
-    const currentRowPcadgId = i + 1
-
-    // 更新行中每个列的 pcadgId
-    tableColumns.value.forEach((col) => {
-      if (tableData.value[i][col.code]) {
-        tableData.value[i][col.code].pcadgId = currentRowPcadgId
-      }
-    })
-  }
-
   // 选中新插入的行
   nextTick(() => {
     currentRow.value = newRow
@@ -764,15 +773,35 @@ const handleUnitChange = (column, newUnit) => {
 }
 
 // 保存
+// 保存 - 在保存时统一更新pcadgId
 const saveTabelDialog = () => {
   // 检查表格数据是否为空
   if (!tableData.value || tableData.value.length === 0) {
     ElMessage.warning("请设置正确的数据")
     return
   }
-  console.log("表格数据:", tableData.value)
+  
+  // 深拷贝原始数据
+  const originalData = JSON.parse(JSON.stringify(tableData.value))
+  
+  // 在拷贝的数据上更新pcadgId
+  const dataToSave = originalData.map((row, rowIndex) => {
+    const newRow = { ...row }
+    tableColumns.value.forEach((col) => {
+      if (newRow[col.code] && typeof newRow[col.code] === "object") {
+        newRow[col.code] = {
+          ...newRow[col.code],
+          pcadgId: rowIndex + 1 // 行号从1开始
+        }
+      }
+    })
+    return newRow
+  })
+
+  console.log("准备保存的表格数据:", dataToSave)
+  
   // 收集所有行数据
-  const formattedData = tableData.value.map((row) => {
+  const formattedData = dataToSave.map((row) => {
     // 收集当前行的所有字段数据
     const rowFields = tableColumns.value.map((col) => {
       const cellData = row[col.code]
@@ -794,13 +823,12 @@ const saveTabelDialog = () => {
   // 最终数据用分号分隔各行
   const result = formattedData.join(";")
 
-  // console.log("格式化后的表格数据:", result);
-
   const params = {
     transCode: "ES0011",
     pcaId: pcaId,
     pcadvals: result
   }
+  
   request(params)
     .then((res) => {
       paneTabledialog.value = false
@@ -808,6 +836,7 @@ const saveTabelDialog = () => {
     })
     .catch((err) => {
       console.error("err", err)
+      // 保存失败,保持原始数据不变
       ElMessage.error("保存失败")
     })
 }