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

feat(handsontable): key value 下拉菜单支持复制粘贴

zhangweicheng 4 лет назад
Родитель
Сommit
e50780b765
2 измененных файлов с 38 добавлено и 4 удалено
  1. 1 1
      handsontable/package.json
  2. 37 3
      handsontable/src/plugins/copyPaste/copyPaste.js

+ 1 - 1
handsontable/package.json

@@ -10,7 +10,7 @@
     "url": "https://github.com/handsontable/handsontable/issues"
   },
   "author": "Handsoncode <hello@handsontable.com>",
-  "version": "6.3.7",
+  "version": "6.3.8",
   "browser": "dist/handsontable.js",
   "main": "commonjs/index.js",
   "module": "es/index.js",

+ 37 - 3
handsontable/src/plugins/copyPaste/copyPaste.js

@@ -215,7 +215,7 @@ class CopyPaste extends BasePlugin {
       const rowSet = [];
 
       arrayEach(copyableColumns, (column) => {
-        rowSet.push(this.hot.getCopyableData(row, column));
+        rowSet.push(this.getCopyData(row, column));
       });
 
       dataSet.push(rowSet);
@@ -253,7 +253,8 @@ class CopyPaste extends BasePlugin {
       const rowSet = [];
 
       arrayEach(copyableColumns, (column) => {
-        rowSet.push(this.hot.getCopyableData(row, column));
+
+        rowSet.push(this.prepareCopyData(row, column));
       });
 
       dataSet.push(rowSet);
@@ -262,6 +263,38 @@ class CopyPaste extends BasePlugin {
     return dataSet;
   }
 
+  // 复制key - value 的dropdown 时 返回 key
+  prepareCopyData(row, column) {
+    let rawData = this.hot.getCopyableData(row, column);
+    const settings = this.hot.getSettings();
+    if (settings && settings.columns) {
+      const colMeta = settings.columns[column];
+      if (colMeta && colMeta.source && colMeta.source.length > 0) {
+        if (typeof colMeta.source[0] === 'object') {
+          colMeta.source.forEach((option) => {
+            if (option.value === rawData) rawData = option.key;
+          });
+        }
+      }
+    }
+    return rawData;
+  }
+
+  // 粘贴key - value 的dropdown 时 返回 value
+  preparePaseData(column, value) {
+    const settings = this.hot.getSettings();
+    if (settings && settings.columns) {
+      const colMeta = settings.columns[column];
+      if (colMeta && colMeta.source && colMeta.source.length > 0) {
+        if (typeof colMeta.source[0] === 'object') {
+          colMeta.source.forEach((option) => {
+            if (option.key === value) value = option.value;
+          });
+        }
+      }
+    }
+    return value;
+  }
   /**
    * Simulates the paste action.
    *
@@ -373,7 +406,8 @@ class CopyPaste extends BasePlugin {
       const newRow = [];
 
       for (let column = startColumn, valuesColumn = 0; column <= endColumn; column += 1) {
-        newRow.push(inputArray[valuesRow][valuesColumn]);
+
+        newRow.push(this.preparePaseData(column, inputArray[valuesRow][valuesColumn]));
 
         valuesColumn = valuesColumn === newValuesMaxColumn ? 0 : valuesColumn += 1;
       }