Bladeren bron

fix(materialModal): 修改表格行键为id,确保唯一性并避免重复选择

xieyong 1 week geleden
bovenliggende
commit
cb5e780c1a

+ 24 - 39
src/views/produce/components/prenatalExamination/material.vue

@@ -6,7 +6,7 @@
       ref="table"
       row-key="id"
       :columns="materialColumns"
-      :datasource="localPickDetails"
+      :datasource="showLocalPickDetails"
       cache-key="prenatal-examination-material-list-table-2511031522"
       autoAmendPage
       :needPage="false"
@@ -72,7 +72,7 @@
       ref="table"
       row-key="id"
       :columns="outputDetailsColumns"
-      :datasource="localOutputDetails"
+      :datasource="showLocalOutputDetails"
       cache-key="prenatal-examination-output-list-table-2511031523"
       autoAmendPage
       :needPage="false"
@@ -348,6 +348,12 @@
       }
     },
     computed: {
+      showLocalPickDetails(){
+        return this.localPickDetails.filter(item => !!item.id);
+      },
+      showLocalOutputDetails() {
+        return this.localOutputDetails.filter(item => !!item.id);
+      },
       // 物料清单表头
       materialColumns() {
         const list = [
@@ -531,45 +537,47 @@
                   count = row.sumReportQuantity;
                 }
 
-                return count + preCount + (row.unit || '');
+                return (count || 0) + preCount + (row.unit || '');
               } else {
                 // 当前行为在制品 和 BOM标准产出
                 const output = this.localOutputDetails.find(
                   (item) => item.categoryCode === row.categoryCode
                 );
 
+                const sumReportQuantity = row.sumReportQuantity || 0;
+
                 if (output) {
                   // 区分已执行状态
                   if (this.executeStatus === 2) {
                     // 已执行,处理重新一键报工时,sumReportQuantity包含了当前报工数量
                     return (
-                      output.sumReportQuantity -
-                      (output.reportQuantityCopy - output.reportQuantity) +
+                      (output.sumReportQuantity -
+                      (output.reportQuantityCopy - output.reportQuantity)) || 0 +
                       (row.unit || '')
                     );
                   }
                   return (
-                    (output.sumReportQuantity || 0) +
-                    output.reportQuantity +
-                    (row.unit || '')
-                  );
+                    ((output.sumReportQuantity || 0) +
+                    output.reportQuantity) ||
+                    0
+                  ) + (row.unit || '');
                 }
 
                 // 产出为在制品 或 BOM标准产出 累计消耗数量
                 if (!this.materialQuotaInfo) {
-                  return row.sumReportQuantity + (row.unit || '');
+                  return sumReportQuantity + (row.unit || '');
                 }
 
                 const materialItem = this.materialQuotaInfo.materialQuota.find(
                   (item) => item.categoryCode === row.categoryCode
                 );
                 if (!materialItem) {
-                  return row.sumReportQuantity + (row.unit || '');
+                  return sumReportQuantity + (row.unit || '');
                 }
                 // 计算在制 标准产出消耗原材料
                 const outputItem = this.localOutputDetails[0];
                 if (!outputItem) {
-                  return row.sumReportQuantity + (row.unit || '');
+                  return sumReportQuantity + (row.unit || '');
                 }
                 // 单位产出所需物料比例
                 const proportion =
@@ -626,9 +634,6 @@
                   cumulativeOutputQuantity * proportion
                 );
 
-                // 已消耗数量转换
-                const sumReportQuantity = row.sumReportQuantity;
-
                 return (
                   needQuantity + preCount + sumReportQuantity + (row.unit || '')
                 );
@@ -637,7 +642,7 @@
             headerSlot: 'sumConsumeQuantityHeader'
           }
         ];
-        if (this.outputType == 1) {
+        // if (this.outputType == 1) {
           list.push({
             prop: '',
             label: '选择',
@@ -646,7 +651,7 @@
             fixed: 'right',
             headerSlot: 'selectColumnHeader'
           });
-        }
+        // }
         return list;
       },
       // 产出清单表头
@@ -846,28 +851,12 @@
         console.log('data', data);
 
         data.forEach((newItem) => {
+          // 用 id 做匹配:id 一样则不动,不存在则插入
           const index = this.localPickDetails.findIndex(
             (item) => item.id === newItem.id
           );
-          if (index !== -1) {
-            // 存在则替换数量
-            this.localPickDetails[index].quantity = newItem.quantity;
-          } else {
-            const sumItem = this.sumOutputDetails.find(
-              (item) => item.categoryCode === newItem.categoryCode
-            );
-
-            let sumQualifiedQuantity = 0;
-            let sumNoQualifiedQuantity = 0;
-            let sumReportQuantity = 0;
-
-            if (sumItem) {
-              sumQualifiedQuantity = sumItem.sumQualifiedQuantity;
-              sumNoQualifiedQuantity = sumItem.sumNoQualifiedQuantity;
-              sumReportQuantity = sumItem.sumReportQuantity;
-            }
+          if (index === -1) {
 
-            // 不存在则添加
             this.localPickDetails.push({
               ...newItem,
               // 物料的类型
@@ -879,9 +868,6 @@
               produceTaskInstanceId: this.produceTaskInstanceId,
               produceTaskName: this.produceTaskName,
               workOrderId: this.workOrderId,
-              sumQualifiedQuantity,
-              sumNoQualifiedQuantity,
-              sumReportQuantity,
               selected: false
             });
           }
@@ -970,7 +956,6 @@
       // 单选
       selectedChange(row) {
         console.log('row', row);
-        if (this.outputType != 1) return;
 
         if (row.selected) {
           // 判断是否存在

+ 2 - 7
src/views/produce/components/prenatalExamination/materialModal.vue

@@ -20,7 +20,7 @@
         :datasource="datasource"
         :selection.sync="selection"
         :need-page="false"
-        row-key="rowKey"
+        row-key="id"
       >
       </ele-pro-table>
     </el-card>
@@ -62,7 +62,7 @@
             selectable: (row, index) => {
               // 已选物料不可再选
               return !this.pickDetails.some(
-                (item) => item.categoryCode === row.categoryCode
+                (item) => item.id === row.id
               );
             }
           },
@@ -154,11 +154,6 @@
           // pageNum: page,
           // size: limit
         });
-        const list = (res && (res.data?.list || res.data || res)) || [];
-        // categoryCode 可能重复(同一物料多工序),拼接 index 合成唯一 rowKey
-        list.forEach((row, idx) => {
-          row.rowKey = `${row.categoryCode || ''}_${idx}`;
-        });
         return res;
       },
       /* 刷新表格 */