Просмотр исходного кода

perf: 物品清单改用原生 textarea,分批入表合并为一次写回

1. 行内多行输入(remark / technicalParams / productionRequirements)改用原生
   textarea:element-ui 的 el-input 在 textarea 模式下 mounted 会跑
   calcTextareaHeight(getComputedStyle + 2 次 scrollHeight 读取),每挂载一个
   单元格就强制整页同步布局一次,几十列 × 上百行时是卡死主因之一
   (实测累计 13.5 秒)。样式对齐 .el-textarea__inner,观感不变。

2. 分批追加改为「只算不写」:新行先攒在普通数组里,全部算完再一次性提交。
   原来每批都替换 form.datasource,会触发 ele-pro-table 深监听 → reload()
   → el-table 整表重建,行内下拉/输入控件被整批重挂(每行 8 个 el-select,
   100 行即 800 个),等于把整表重建 N 遍(O(n²))。改为一次写回后
   el-table 只做 1 遍全表 diff + 布局。

3. 抽出 recalculateRow:changeCount 内部是 JSON 深拷贝,必须返回新对象,
   不能就地改原行。批量重算与分批追加共用同一份派生字段逻辑。
xieyong 2 недель назад
Родитель
Сommit
45d699ac08
1 измененных файлов с 97 добавлено и 28 удалено
  1. 97 28
      src/BIZComponents/inventoryTable.vue

+ 97 - 28
src/BIZComponents/inventoryTable.vue

@@ -306,11 +306,18 @@
       </template> -->
         <template v-slot:remark="scope">
           <el-form-item :prop="'datasource.' + scope.$index + '.remark'">
-            <el-input
+            <!-- 用原生 textarea 代替 el-input type="textarea"。
+                 element-ui 的 el-input 在 textarea 模式下,mounted 时会跑
+                 calcTextareaHeight(getComputedStyle + 2 次 scrollHeight 读取),
+                 每挂载一个单元格就强制浏览器整页同步布局一次。
+                 表格几十列 × 上百行时这是卡死的主因之一(实测累计 13.5 秒)。
+                 原生 textarea 不做挂载期测量,多行输入能力保留。 -->
+            <textarea
               v-model="scope.row.remark"
-              type="textarea"
+              class="cell-textarea"
+              rows="2"
               placeholder="请输入"
-            ></el-input>
+            ></textarea>
           </el-form-item>
         </template>
         <template v-slot:singlePrice="scope">
@@ -379,11 +386,13 @@
           <el-form-item
             :prop="'datasource.' + scope.$index + '.technicalParams'"
           >
-            <el-input
-              type="textarea"
+            <!-- 同 remark 栏:原生 textarea 去掉 element-ui 挂载期的强制布局 -->
+            <textarea
               v-model="scope.row.technicalParams"
+              class="cell-textarea"
+              rows="2"
               placeholder="请输入"
-            ></el-input>
+            ></textarea>
           </el-form-item>
         </template>
         <template v-slot:technicalDrawings="scope">
@@ -985,12 +994,13 @@
           <el-form-item
             :prop="'datasource.' + $index + '.productionRequirements'"
           >
-            <el-input
+            <!-- 同 remark 栏:原生 textarea 去掉 element-ui 挂载期的强制布局 -->
+            <textarea
               v-model="row.productionRequirements"
+              class="cell-textarea"
+              rows="2"
               placeholder="请输入"
-              type="textarea"
-            >
-            </el-input>
+            ></textarea>
           </el-form-item>
         </template>
         <template v-slot:cuttingLength="{ row, $index }">
@@ -2908,6 +2918,23 @@
 
         return row;
       },
+      /**
+       * 重算单行:数量换算、折后单价、含税/未税单价等派生字段。
+       * changeCount 内部是 JSON 深拷贝,必须返回新对象,不能就地改原行。
+       */
+      recalculateRow(item) {
+        const row = changeCount(item, this.countObj, false);
+        if (row.pricingWay == 3) {
+          row.totalPrice = formatPrice(
+            (row.increaseTotalWeight || 0) * row.singlePrice
+          );
+        }
+        row.notaxSinglePrice =
+          row.singlePrice && row.taxRate
+            ? formatPrice(row.singlePrice / (1 + row.taxRate / 100))
+            : '';
+        return row;
+      },
       /**
        * 重算表格。dirtyIndexes 只指定本次真正变动的行下标,
        * 其余行直接复用原对象引用,省掉深拷贝与重复观测的成本。
@@ -2921,17 +2948,7 @@
           if (dirty && !dirty.has(index)) {
             return item;
           }
-          const row = changeCount(item, this.countObj, false);
-          if (row.pricingWay == 3) {
-            row.totalPrice = formatPrice(
-              (row.increaseTotalWeight || 0) * row.singlePrice
-            );
-          }
-          row.notaxSinglePrice =
-            row.singlePrice && row.taxRate
-              ? formatPrice(row.singlePrice / (1 + row.taxRate / 100))
-              : '';
-          return row;
+          return this.recalculateRow(item);
         });
         this.changeAll();
       },
@@ -2997,8 +3014,16 @@
       },
       /**
        * 分批追加:一次选中上百个物品时,一次性塞进表格会让主线程长时间阻塞,
-       * 这里按 appendBatchSize 分批写入并重算,每批之间让出一帧,
-       * 浏览器可以正常绘制 loading 遮罩和已经渲染出来的行。
+       * 这里按 appendBatchSize 分批计算,批与批之间让出一帧,
+       * 浏览器可以正常绘制 loading 遮罩和进度。
+       *
+       * 关键:分批只用来「算」,不用来「写」。
+       * 每次替换 form.datasource 都会触发 ele-pro-table 的 datasource 深监听 →
+       * reload() → tableData 换引用 → el-table 整表重建,
+       * 行内的下拉/输入控件会被整批重新挂载(每行 8 个 el-select,
+       * 100 行就是 800 个),一次挂载就要读一次几何尺寸并强制整文档重排。
+       * 分批写回等于把整表重建 N 遍(O(n²)),
+       * 所以新行先攒在普通数组里,全部算完再一次性提交,只重建 1 遍。
        *
        * options.recalculate = false 时不重算本批行:
        * 修改回显的是用户已经保存过的数据,不能按新增的逻辑重算价格/数量。
@@ -3023,15 +3048,19 @@
           this.loadedCount = 0;
           this.totalCount = total;
           let done = 0;
+          // 新行先攒在普通数组里:还没进响应式数据源,赋值零成本,
+          // 也不会触发任何表格重建。
+          const pending = [];
 
           const step = () => {
             const batch = rows.slice(done, done + batchSize);
             if (batch.length) {
-              const from = startIndex + done;
-              this.form.datasource = this.form.datasource.concat(batch);
-              // 只重算本批新增的行,其余行复用原对象
+              // 只算不写:新行先进普通数组,还没进响应式数据源,
+              // 赋值零成本,也不会触发任何表格重建。
               if (shouldRecalculate) {
-                this.recalculateTableOnce(batch.map((row, i) => from + i));
+                pending.push(...batch.map((row) => this.recalculateRow(row)));
+              } else {
+                pending.push(...batch);
               }
               done += batch.length;
               this.loadedCount = done;
@@ -3040,7 +3069,16 @@
               requestAnimationFrame(step);
               return;
             }
-            // 全部批次都写完并渲染完,才收尾关闭 loading
+            // 全部批次都算完后一次性写回:
+            // datasource 只换一次引用 -> ele-pro-table 只 reload 1 次 ->
+            // el-table 只做 1 遍全表 diff + 布局,而不是每批 1 遍。
+            if (pending.length) {
+              this.form.datasource = this.form.datasource
+                .slice(0, startIndex)
+                .concat(pending);
+              this.changeAll();
+            }
+            // 全部批次都算完并写回,才收尾关闭 loading
             this.batchLoading = false;
             if (onDone) onDone();
             this.endTableLoading();
@@ -3351,6 +3389,37 @@
   };
 </script>
 <style lang="scss" scoped>
+  // 行内多行输入:原生 textarea 替代 el-input type="textarea",
+  // 样式对齐 element-ui 的 el-textarea__inner,保证观感一致
+  .cell-textarea {
+    width: 100%;
+    flex: 1 1 100%;
+    min-width: 0;
+    box-sizing: border-box;
+    padding: 4px 8px;
+    font-size: 12px;
+    line-height: 18px;
+    font-family: inherit;
+    color: #606266;
+    background-color: #ffffff;
+    border: 1px solid #dcdfe6;
+    border-radius: 4px;
+    resize: vertical;
+    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
+
+    &:focus {
+      outline: none;
+      border-color: #409eff;
+    }
+
+    &:disabled {
+      background-color: #f5f7fa;
+      border-color: #e4e7ed;
+      color: #c0c4cc;
+      cursor: not-allowed;
+    }
+  }
+
   .headbox {
     display: flex;
     justify-content: space-between;