浏览代码

修复物料明细更新时订单总计不重新计算的问题

- 修复avue-crud组件中scope.
- 添加findRowIndex方法作为索引查找的备用方案
- 优化handleMaterialUpdate方法,确保总是重新计算订单总计
- 移除调试语句,代码清理完成
yz 2 周之前
父节点
当前提交
2d53c4e61a
共有 2 个文件被更改,包括 66 次插入7 次删除
  1. 62 3
      src/components/order-form/material-detail-table.vue
  2. 4 4
      src/components/order-form/order-form.vue

+ 62 - 3
src/components/order-form/material-detail-table.vue

@@ -58,7 +58,7 @@
             style="width: 100%"
             placeholder="请输入单价"
             @input="validateFloatInput($event, scope.row, 'unitPrice')"
-            @blur="validateAndFormatFloatOnBlur(scope.row, 'unitPrice'); handleUnitPriceChange(scope.row, scope.$index)"
+            @blur="handleUnitPriceBlur(scope.row, scope.$index)"
           />
           <span v-else>{{ formatUnitPrice(scope.row.unitPrice) }}</span>
         </template>
@@ -71,7 +71,7 @@
             style="width: 100%"
             placeholder="请输入订单数量"
             @input="validateIntegerInput($event, scope.row, 'orderQuantity')"
-            @blur="handleQuantityChange(scope.row, scope.$index)"
+            @blur="handleQuantityBlur(scope.row, scope.$index)"
           />
           <span v-else>{{ formatIntegerNumber(scope.row.orderQuantity) }}</span>
         </template>
@@ -670,6 +670,19 @@ export default {
     },
 
     /**
+     * 处理订单数量失焦事件
+     * @description 当订单数量输入框失焦时,触发数量变更处理
+     * @param {MaterialDetailRecord} row - 行数据
+     * @param {number} index - 行索引
+     * @returns {void}
+     */
+    handleQuantityBlur(row, index) {
+      // 如果 index 无效,尝试通过 row 数据找到正确的索引
+      const actualIndex = this.findRowIndex(row, index)
+      this.handleQuantityChange(row, actualIndex)
+    },
+
+    /**
      * 处理订单数量变更
      * @description 当订单数量发生变化时,自动计算总金额和税额,并触发父组件重新计算订单总计
      * @param {MaterialDetailRecord} row - 行数据
@@ -695,6 +708,22 @@ export default {
     },
 
     /**
+     * 处理单价失焦事件
+     * @description 当单价输入框失焦时,先格式化数值,再计算总金额和税额,并触发父组件重新计算订单总计
+     * @param {MaterialDetailRecord} row - 行数据
+     * @param {number} index - 行索引
+     * @returns {void}
+     */
+    handleUnitPriceBlur(row, index) {
+      // 先格式化数值
+      this.validateAndFormatFloatOnBlur(row, 'unitPrice')
+      // 如果 index 无效,尝试通过 row 数据找到正确的索引
+      const actualIndex = this.findRowIndex(row, index)
+      // 再处理单价变更
+      this.handleUnitPriceChange(row, actualIndex)
+    },
+
+    /**
      * 处理单价变更
      * @description 当单价发生变化时,自动计算总金额和税额,并触发父组件重新计算订单总计
      * @param {MaterialDetailRecord} row - 行数据
@@ -829,7 +858,37 @@ export default {
      */
     getCurrentRowIndex(row) {
       return this.currentPageData.findIndex(item => item.id === row.id)
-    }
+    },
+
+    /**
+      * 查找行索引
+      * @description 根据行数据查找在物料明细列表中的正确索引
+      * @param {MaterialDetailRecord} row - 行数据
+      * @param {number} providedIndex - 提供的索引
+      * @returns {number} 实际索引
+      */
+     findRowIndex(row, providedIndex) {
+       // 如果提供的索引有效,直接使用
+       if (providedIndex >= 0 && providedIndex < this.materialDetails.length) {
+         return providedIndex
+       }
+       
+       // 否则通过行数据查找索引
+       const index = this.materialDetails.findIndex(item => {
+         // 优先使用 id 进行匹配
+         if (row.id && item.id) {
+           return row.id === item.id
+         }
+         // 如果没有 id,使用物料编码进行匹配
+         if (row.itemCode && item.itemCode) {
+           return row.itemCode === item.itemCode
+         }
+         // 最后使用对象引用进行匹配
+         return row === item
+       })
+       
+       return index >= 0 ? index : -1
+     }
   }
 }
 </script>

+ 4 - 4
src/components/order-form/order-form.vue

@@ -263,13 +263,13 @@ export default {
      * @returns {void}
      */
     handleMaterialUpdate({ row, index }) {
-      // 更新物料明细列表中对应的记录
+      // 如果有有效的索引,更新物料明细列表中对应的记录
       if (index >= 0 && index < this.materialDetails.length) {
         this.$set(this.materialDetails, index, { ...row })
-        
-        // 重新计算订单总金额
-        this.calculateOrderTotal()
       }
+      
+      // 无论索引是否有效,都重新计算订单总金额
+      this.calculateOrderTotal()
     },
 
     /**