實現 el-table 中鍵盤方向鍵導航功能vue2+vue3(類似 Excel)

實現 el-table 中鍵盤方向鍵導航功能vue2+vue3(類似 Excel)

功能需求

在 Element UI 的 el-table 表格中實現以下功能:

  • 使用鍵盤上下左右鍵在可編輯的 el-input/el-select 之間移動焦點
  • 焦點移動時自動定位到對應單元格
  • 支持光標位置自動調整,提升編輯體驗
完整解決方案(vue2)
1. 表格結構修改

在 el-table 中添加鍵盤事件監聽,并為可編輯元素添加定位標識:

<template><el-table :data="tableData" border style="width: 100%" size="small"><el-table-column prop="account_id" label="結算賬戶" width="180" align="center"><template slot-scope="scope"><el-select v-model="scope.row.account_id" placeholder="請選擇賬戶" clearable filterable @keydown.native.stop="onKeyDown(scope.$index, 'account_id', $event)" :ref="getInputRef(scope.$index, 'account_id')"><el-option label="a" value="1" /><el-option label="b" value="2" /></el-select></template></el-table-column><el-table-column prop="money" label="結算金額" width="180" align="center"><template slot-scope="scope"><el-input v-model="scope.row.money" clearable @keydown.native.stop="onKeyDown(scope.$index, 'money', $event)" :ref="getInputRef(scope.$index, 'money')"/></template></el-table-column><el-table-column prop="remark" label="備注" align="center"><template slot-scope="scope"><el-input v-model="scope.row.remark" @keydown.native.stop="onKeyDown(scope.$index, 'remark', $event)" :ref="getInputRef(scope.$index, 'remark')"></el-input></template></el-table-column></el-table>
</template>
2. 核心 JavaScript 邏輯

在 Vue 組件的 methods 中添加焦點導航控制方法:

<script>
export default {data() {return {tableData: [{},{},{},{}],columns: [{prop: 'account_id'},{prop: 'money'},{prop: 'remark'}],refList: {}}},methods: {getInputRef(rowIndex, columnProp) {return `input-${rowIndex}-${columnProp}`},onKeyDown(rowIndex, columnProp, event) {// 當前列在columns數組中的索引const columnIndex = this.columns.findIndex((c) => c.prop === columnProp)// 計算下一個輸入框的位置,如果是當前行的最后一個輸入框則移到下一行的第一個輸入框let nextColumnIndex = (columnIndex + 1) % this.columns.length;let nextRowIndex;switch(event.keyCode) {case 38:nextRowIndex = rowIndex - 1;nextColumnIndex = columnIndex;break;case 40:nextRowIndex = rowIndex + 1;nextColumnIndex = columnIndex;break;case 37:nextRowIndex = columnIndex === 0 ? rowIndex - 1 : rowIndexnextColumnIndex = columnIndex === 0 ? this.columns.length - 1 : columnIndex - 1break;case 39:nextRowIndex = columnIndex === this.columns.length - 1 ? rowIndex + 1 : rowIndexbreak;}const nextInputRef = `input-${nextRowIndex}-${this.columns[nextColumnIndex].prop}`const currentInputRef = `input-${rowIndex}-${this.columns[columnIndex].prop}`this.$nextTick(() => {if (this.$refs[nextInputRef]) {this.$refs[nextInputRef].focus()if (this.$refs[currentInputRef]) {this.$refs[currentInputRef].blur()}}})}}
}
</script>
完整解決方案(vue3)
 <template><el-table :data="tableData" border style="width: 100%" size="small"><el-table-column prop="account_id" label="結算賬戶" width="180" align="center"><template #default="{ row, $index,column }"><el-select v-model="row.account_id" placeholder="請選擇賬戶" clearable filterable @keydown="onKeyDown($index,column.property,$event)" :ref="(el)=>setRef(el,`input-${$index}-${column.property}`)"><el-option label="a" value="1" /><el-option label="b" value="2" /></el-select></template></el-table-column><el-table-column prop="money" label="結算金額" width="180" align="center"><template #default="{ row, $index,column }"><el-input v-model="row.money" clearable @keydown="onKeyDown($index,column.property,$event)" :ref="(el)=>setRef(el,`input-${$index}-${column.property}`)"/></template></el-table-column><el-table-column prop="remark" label="備注" align="center"><template #default="{ row,$index,column }"><el-input v-model="row.remark" @keydown="onKeyDown($index,column.property,$event)" :ref="(el)=>setRef(el,`input-${$index}-${column.property}`)"></el-input></template></el-table-column></el-table>
</template>
<script setup>
import { nextTick, ref } from 'vue'const tableData = [{},{},{},{}
]const columns = [{prop:'account_id'},{prop:'money'},{prop:'remark'}
]
const refList = ref({})
const setRef = (el,key)=>{refList.value[key] = el
}
function onKeyDown(rowIndex,columnProp,event){// 當前列在columns數組中的索引const columnIndex = columns.findIndex((c) => c.prop === columnProp)// 計算下一個輸入框的位置,如果是當前行的最后一個輸入框則移到下一行的第一個輸入框let nextColumnIndex = (columnIndex + 1) % columns.length;let nextRowIndex;switch(event.keyCode){case 38:nextRowIndex = rowIndex - 1 ;nextColumnIndex = columnIndex;break;case 40:nextRowIndex = rowIndex + 1 ;nextColumnIndex = columnIndex;break;case 37:nextRowIndex = columnIndex === 0 ? rowIndex - 1 : rowIndexnextColumnIndex = columnIndex === 0 ? columns.length - 1 : columnIndex - 1break;case 39:nextRowIndex = columnIndex === columns.length - 1 ? rowIndex + 1 : rowIndexbreak;}const nextInputRef = `input-${nextRowIndex}-${columns[nextColumnIndex].prop}`const currentInputRef = `input-${rowIndex}-${columns[columnIndex].prop}`nextTick(() => {if (refList.value[nextInputRef]) {refList.value[nextInputRef].focus()refList.value[currentInputRef].blur()}})}
</script>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/86013.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/86013.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/86013.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MyBatis:從入門到進階

&#x1f4cc; 摘要 在 Java 后端開發中&#xff0c;MyBatis 是一個非常流行且靈活的持久層框架。它不像 Hibernate 那樣完全封裝 SQL&#xff0c;而是提供了對 SQL 的精細控制能力&#xff0c;同時又具備 ORM&#xff08;對象關系映射&#xff09;的功能。 本文將帶你從 MyB…

leetcode51.N皇后:回溯算法與沖突檢測的核心邏輯

一、題目深度解析與N皇后問題本質 題目描述 n皇后問題研究的是如何將n個皇后放置在nn的棋盤上&#xff0c;并且使皇后彼此之間不能相互攻擊。給定一個整數n&#xff0c;返回所有不同的n皇后問題的解決方案。每一種解法包含一個明確的n皇后問題的棋子放置方案&#xff0c;該方…

算法-每日一題(DAY9)楊輝三角

1.題目鏈接&#xff1a; 118. 楊輝三角 - 力扣&#xff08;LeetCode&#xff09; 2.題目描述&#xff1a; 給定一個非負整數 numRows&#xff0c;生成「楊輝三角」的前 numRows 行。 在「楊輝三角」中&#xff0c;每個數是它左上方和右上方的數的和。 示例 1: 輸入: numRo…

【MATLAB代碼】制導方法介紹與例程——追蹤法,適用于二維平面,目標是移動的|附完整源代碼

追蹤法(追蹤導引法)是一種常見的導彈導引方式,其基本原理是保持導彈的速度矢量始終指向目標。在追蹤法中,導彈的加速度可以表示為指向目標的加速度。 本文給出二維平面下,移動目標的追蹤法導引的介紹、公式與matlab例程 訂閱專欄后,可以直接查看完整源代碼 文章目錄 運行…

小白的進階之路系列之十八----人工智能從初步到精通pytorch綜合運用的講解第十一部分

從零開始的NLP:使用序列到序列網絡和注意力機制進行翻譯 我們將編寫自己的類和函數來預處理數據以完成我們的 NLP 建模任務。 在這個項目中,我們將訓練一個神經網絡將法語翻譯成英語。 [KEY: > input, = target, < output]> il est en train de peindre un table…

SSL安全證書:數字時代的網絡安全基石

SSL安全證書&#xff1a;數字時代的網絡安全基石 在當今數字化浪潮中&#xff0c;網絡通信安全已成為個人、企業和組織不可忽視的核心議題。SSL&#xff08;Secure Sockets Layer&#xff0c;安全套接層&#xff09;安全證書作為保障數據傳輸安全的關鍵技術&#xff0c;通過加…

LLM-201: OpenHands與LLM交互鏈路分析

一、核心交互鏈路架構 #mermaid-svg-ZBqCSQk1PPDkIXNx {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ZBqCSQk1PPDkIXNx .error-icon{fill:#552222;}#mermaid-svg-ZBqCSQk1PPDkIXNx .error-text{fill:#552222;strok…

【項目】仿muduo庫one thread one loop式并發服務器SERVER模塊(下)

&#x1f4da; 博主的專欄 &#x1f427; Linux | &#x1f5a5;? C | &#x1f4ca; 數據結構 | &#x1f4a1;C 算法 | &#x1f152; C 語言 | &#x1f310; 計算機網絡 |&#x1f5c3;? mysql 項目文章&#xff1a; 仿muduo庫one thread one loop式并發服務器…

數據庫索引結構 B 樹、B + 樹與哈希索引在不同數據查詢場景下的適用性分析

一、數據庫索引結構B樹 樹概述 樹是一種多路平衡查找樹&#xff0c;廣泛應用于數據庫和文件系統中。B樹的節點可以存儲多個數據元素&#xff0c;并且保持樹的平衡&#xff0c;以提高查詢效率。 適用性分析 在數據量較大&#xff0c;范圍查找較多的場景下&#xff0c;B樹的查詢效…

Linux進程間通信——信號

1.信號的介紹 信號( Signal )是 Unix, 類Unix以及其他POSIX兼容的操作系統中進程間通信的一種有限制的手段。 1&#xff09;信號是在軟件層面上對中斷機制的一種模擬&#xff0c;是一種異步通信方式。2&#xff09;信號可以直接進行用戶空間進程和內核進程之間的交互&#xff…

Bytemd@Bytemd/react詳解(編輯器實現基礎AST、插件、跨框架)

ByteMD Markdown編輯器詳細解釋&修改編輯器默認樣式&#xff08;高度300px) AST樹詳解 [ByteMD 插件系統詳解(https://blog.csdn.net/m0_55049655/article/details/148811248?spm1001.2014.3001.5501) Sevelet編寫的Bytemd如何適配到React中 ??1?? 背景概述 Byte…

《Redis》事務

文章目錄 Redis中的原子性Redis的事物和MySQL事務的區別Redis實現事務什么場景下&#xff0c;會使用事務? Redis事務相關命令watch命令的實現原理 總結 Redis中的原子性 Redis的原子性不同于MySQL的原子性。 Redis的事物和MySQL事務的區別 但是注意體會Redis的事務和MySQL…

Elasticsearch Kibana (一)

一、官方文檔 elasticsearch官網&#xff1a;elasticsearch官網 elasticsearch源碼&#xff1a;elasticsearch源碼 ik分詞器&#xff1a; ik分詞器 ik分詞器下載&#xff1a;ik分詞器下載 elasticsearch 版本選擇&#xff1a;elasticsearch 版本選擇 官方推薦Elasticsearch和j…

[linux] Ubuntu 24軟件下載和安裝匯總(自用)

經常重裝系統&#xff0c;備份下&#xff0c;有用的也可以參考。 安裝圖形界面 apt install ubuntu-desktop systemctl set-default graphical.target reboot 安裝chrome wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo apt insta…

分布變化的模仿學習算法

與傳統監督學習不同,直接模仿學習在不同時刻所面臨的數據分布可能不同.試設計一個考慮不同時刻數據分布變化的模仿學習算法 import numpy as np import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset from…

arm-none-eabi-ld: cannot find -lm

arm-none-eabi-ld -Tuser/hc32l13x.lds -o grbl_hc32l13x.elf user/interrupts_hc32l13x.o user/system_hc32l13x.o user/main.o user/startup_hc32l13x.o -lm -Mapgrbl_hc32l13x.map arm-none-eabi-ld: cannot find -lm makefile:33: recipe for target link failed 改為在gcc…

【Python辦公】Excel文件批量樣式修改器

目錄 專欄導讀1. 背景介紹2. 項目概述3. 庫的安裝4. 核心架構設計① 類結構設計② 數據模型1) 文件管理2) 樣式配置5. 界面設計與實現① 布局結構② 動態組件生成6. 核心功能實現① 文件選擇與管理② 顏色選擇功能③ Excel文件處理核心邏輯完整代碼結尾專欄導讀 ?? 歡迎來到P…

QT的一些介紹

//雖然下面一行代碼進行widget和ui的窗口關聯&#xff0c;但是如果發生窗口大小變化的時候&#xff0c;里面的布局不會隨之變化ui->setupUi(this);//通過下面這行代碼進行顯示說明&#xff0c;讓窗口變化時&#xff0c;布局及其子控件隨之變化this->setLayout(ui->ver…

RISC-V向量擴展與GPU協處理:開源加速器設計新范式——對比NVDLA與香山架構的指令集融合方案

點擊 “AladdinEdu&#xff0c;同學們用得起的【H卡】算力平臺”&#xff0c;H卡級別算力&#xff0c;按量計費&#xff0c;靈活彈性&#xff0c;頂級配置&#xff0c;學生專屬優惠 當開源指令集遇上異構計算&#xff0c;RISC-V向量擴展&#xff08;RVV&#xff09;正重塑加速…

自動恢復網絡路由配置的安全腳本說明

背景 兩個文章 看了就明白 Ubuntu 多網卡路由配置筆記&#xff08;內網 外網同時通 可能有問題&#xff0c;以防萬一可以按照個來恢復 sudo ip route replace 192.168.1.0/24 dev eno8403 proto kernel scope link src <你的IP>或者恢復腳本! 如下 誤操作路由時&…