实现 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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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>或者恢复脚本! 如下 误操作路由时&…