瀏覽代碼

feat(handsontable): 如果列存在decimal配置,粘贴数值字符串时,将其转换为number并根据小数位数进行四舍五入

vian 4 年之前
父節點
當前提交
0f682d11f4
共有 2 個文件被更改,包括 8 次插入1 次删除
  1. 1 0
      handsontable/package.json
  2. 7 1
      handsontable/src/plugins/copyPaste/copyPaste.js

+ 1 - 0
handsontable/package.json

@@ -59,6 +59,7 @@
     "data-spreadsheet"
     "data-spreadsheet"
   ],
   ],
   "dependencies": {
   "dependencies": {
+    "@sc/util": "^1.0.7",
     "moment": "2.20.1",
     "moment": "2.20.1",
     "numbro": "^2.0.6",
     "numbro": "^2.0.6",
     "pikaday": "1.5.1"
     "pikaday": "1.5.1"

+ 7 - 1
handsontable/src/plugins/copyPaste/copyPaste.js

@@ -1,3 +1,4 @@
+import { roundForObj, isNumber } from '@sc/util';
 import BasePlugin from './../_base';
 import BasePlugin from './../_base';
 import Hooks from './../../pluginHooks';
 import Hooks from './../../pluginHooks';
 import SheetClip from './../../../lib/SheetClip/SheetClip';
 import SheetClip from './../../../lib/SheetClip/SheetClip';
@@ -280,17 +281,22 @@ class CopyPaste extends BasePlugin {
     return rawData;
     return rawData;
   }
   }
 
 
-  // 粘贴key - value 的dropdown 时 返回 value
+  // 粘贴数据预处理
   preparePaseData(column, value) {
   preparePaseData(column, value) {
     const settings = this.hot.getSettings();
     const settings = this.hot.getSettings();
     if (settings && settings.columns) {
     if (settings && settings.columns) {
       const colMeta = settings.columns[column];
       const colMeta = settings.columns[column];
       if (colMeta && colMeta.source && colMeta.source.length > 0) {
       if (colMeta && colMeta.source && colMeta.source.length > 0) {
+        // 粘贴key - value 的dropdown 时 返回 value
         if (typeof colMeta.source[0] === 'object') {
         if (typeof colMeta.source[0] === 'object') {
           colMeta.source.forEach((option) => {
           colMeta.source.forEach((option) => {
             if (option.key === value) value = option.value;
             if (option.key === value) value = option.value;
           });
           });
         }
         }
+      } else if (isNumber(value) && ['number', 'function'].includes(typeof colMeta.decimal)) {
+        // 粘贴数值字符串时,将其转换为number并根据小数位数进行四舍五入
+        const decimalNum = typeof colMeta.decimal === 'function' ? colMeta.decimal() : colMeta.decimal;
+        value = roundForObj(value, decimalNum);
       }
       }
     }
     }
     return value;
     return value;