Procházet zdrojové kódy

键盘多个复制粘贴

ZaiZai před 2 roky
rodič
revize
0ba543fbba
2 změnil soubory, kde provedl 72 přidání a 17 odebrání
  1. 70 15
      src/components/table-form/index.vue
  2. 2 2
      src/utils/tools.js

+ 70 - 15
src/components/table-form/index.vue

@@ -298,10 +298,11 @@ const copyKeyList = ref(getStoreData('TableFormCopyKeyList') || [])
 //设置选择数据
 const setShiftTableForm = (key) => {
     if (isCtrlKey.value) {
+        const form = tableFormInfo.value
         const keys = checkKeyList.value
         const index = getIndex(keys, 'key', key)
         if (index === -1) {
-            keys.push({key: key})
+            keys.push({key: key, val: form[key]})
         } else {
             keys.splice(index, 1)
         }
@@ -311,7 +312,7 @@ const setShiftTableForm = (key) => {
 }
 
 //全局按键按下监听
-document.onkeydown = (event) => {
+document.onkeydown = async (event) => {
     const {key, ctrlKey} = event
     //按下ctrl键
     if (ctrlKey && key === 'Control') {
@@ -321,6 +322,7 @@ document.onkeydown = (event) => {
     if (ctrlKey && key === 'c') {
         const keysList = deepClone(checkKeyList.value)
         if (keysList.length > 0) {
+            event.preventDefault()
             setStoreData('TableFormCopyKeyList', keysList)
             copyKeyList.value = keysList
             keysList.forEach(item => {
@@ -334,19 +336,19 @@ document.onkeydown = (event) => {
         const keysList = deepClone(copyKeyList.value)
         const checkList = checkKeyList.value
         if (checkList.length > 0) {
-            const form = tableFormInfo.value
-            checkList.forEach((item) => {
-                const form_key = item['key']
-                const item_value = form[form_key]
-                if (keysList.length > 0) {
-                    const val = form[keysList[0]['key']]
-                    tableFormInfo.value[form_key] = val ? val : item_value
-                    keysList.splice(0, 1) //删除第一个元素
-                } else {
-                    tableFormInfo.value[form_key] = item_value
-                }
-                HTableForm.setCheckKeyStyle(form_key, true)
-            })
+            event.preventDefault()
+            //粘贴多个
+            if (checkList.length > 1 && keysList.length > 1) {
+                await setMultipleCheckValue(checkList, keysList)
+            }
+            //粘贴单个复制
+            if (checkList.length > 1 && keysList.length === 1) {
+                await setSingleCopyValue(checkList, keysList)
+            }
+            //复制单个粘贴
+            if (checkList.length === 1 && keysList.length > 1) {
+                await setCopySingleValue(checkList, keysList)
+            }
             //清除缓存
             checkKeyList.value = []
             copyKeyList.value = []
@@ -355,6 +357,59 @@ document.onkeydown = (event) => {
     }
 }
 
+//复制单个粘贴
+const setCopySingleValue = async (checkList, keysList) => {
+    let form_val = '', key = checkList[0]['key']
+    const form = tableFormInfo.value
+    for (let i = 0; i < keysList.length; i++) {
+        const val = form[keysList[i]['key']]
+        form_val = form_val ? form_val + '、' + val : val
+        keysList.splice(0, 1) //删除第一个元素
+    }
+    await setTableFormInfoValue(key, form_val)
+    HTableForm.setCheckKeyStyle(key, true)
+}
+
+//粘贴单个复制
+const setSingleCopyValue = async (checkList, keysList) => {
+    const form = tableFormInfo.value
+    const form_val = form[keysList[0]['key']]
+    for (let i = 0; i < checkList.length; i++) {
+        const {key} = checkList[i]
+        await setTableFormInfoValue(key, form_val)
+        HTableForm.setCheckKeyStyle(key, true)
+    }
+    keysList.splice(0, 1) //删除第一个元素
+}
+
+
+//粘贴多个
+const setMultipleCheckValue = async (checkList, keysList) => {
+    const form = tableFormInfo.value
+    for (let i = 0; i < checkList.length; i++) {
+        const {key, val} = checkList[i]
+        if (keysList.length > 0) {
+            const form_val = form[keysList[0]['key']]
+            await setTableFormInfoValue(key, form_val ? form_val : val)
+            keysList.splice(0, 1) //删除第一个元素
+        } else {
+            await setTableFormInfoValue(key, val)
+        }
+        HTableForm.setCheckKeyStyle(key, true)
+    }
+}
+
+//设置表单数据
+const setTableFormInfoValue = async (key, value) => {
+    return new Promise((resolve) => {
+        setTimeout(() => {
+            tableFormInfo.value[key] = value
+            resolve(true)
+        }, 100)
+    })
+}
+
+
 //全局键盘放开监听
 document.onkeyup = (event) => {
     const {key, ctrlKey} = event

+ 2 - 2
src/utils/tools.js

@@ -49,10 +49,10 @@ export const rowsToId = (rows) => {
 }
 
 //拼接字段
-export const rowsToKey = (rows, key) => {
+export const rowsToKey = (rows, key, join = ',') => {
     return rows.map((obj) => {
         return obj[key];
-    }).join(",")
+    }).join(join)
 }
 
 //删除提醒