当前位置: 首页 > wzjs >正文

青岛 网站设计国家再就业免费培训网

青岛 网站设计,国家再就业免费培训网,云南 房地产网站建设,北京装饰公司十大排名实现 el-table 中键盘方向键导航功能vue2vue3(类似 Excel) 功能需求 在 Element UI 的 el-table 表格中实现以下功能: 使用键盘上下左右键在可编辑的 el-input/el-select 之间移动焦点焦点移动时自动定位到对应单元格支持光标位置自动调整…

实现 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.dtcms.com/wzjs/75553.html

相关文章:

  • 班级展示网站百度推广培训
  • 做网站如何接单河南seo快速排名
  • 沈阳企业自助建站系统网络营销推广的
  • 茶叶网站开发目的和意义北京seo外包公司要靠谱的
  • 黑龙江建设网站广东全网推广
  • 网站收录不增加品牌推广方案模板
  • 日本建设网站北京seo优化排名
  • 建设个人网站用什么软件好dw网页制作教程
  • 钓鱼网站实施过程win优化大师怎么样
  • 苏州专业网站设计比较好的网络优化公司
  • 网站设计网络推广网站流量分析工具
  • 做书的网站有哪些内容如何做推广宣传
  • 网站运营 网站建设百度云资源搜索入口
  • 上海最好的网站设计公司2022智慧树互联网与营销创新
  • 建设一个电影网站怎么做优书网首页
  • 仿木鱼网络网站互联网营销案例
  • 知名网站建设在哪里宁夏百度公司
  • 集团网站建设案例seo营销技巧
  • 莱州相亲网站苏州网站制作推广
  • 橙子建站是什么软件营销型网站建设步骤
  • 响应式网页设计网站建设优化大师官方免费
  • 新网站制作公司互动营销平台
  • 可以做微信游戏的网站有哪些上海比较大的优化公司
  • 做网站平台的公司有哪些新手如何找cps推广渠道
  • 湖南有实力竞价优化服务信息流广告优化师
  • baby做网站汽车百度app下载官方免费下载安装
  • 微信网站建设电话seo关键词优化最多可以添加几个词
  • 东莞南城做网站上海不限关键词优化
  • 外贸网站外链怎么做新网域名注册官网
  • 淘宝客怎么做的网站公司的网站制作