vue3+Element-Plus+js 解决表格按住上下左右获取焦点

问题:

        一个el-table表格里面每一个框框都是一个el-input 然后给输入框v-model绑定值,绑定完成之后我们会发现有这么一个问题,不能按住上下左右键盘来移动,这时候我们操作就来了,效果如下:

 步骤走起:代码不做解释,开发时间久了就懂了

import { nextTick } from 'vue'
export class DirectionKey {
    constructor(str, code) {
        let _that = this
        str.split('_').forEach(item => {
            let o = item.split('=')
            _that[o[0]] = o[1]
        })
        if (code === 38) {
            this.previousLine()
        }
        if (code === 40) {
            this.nextLine()
        }
        if (code === 39) {
            this.next()
        }
        if (code === 37) {
            this.previous()
        }
    }
    next(x = this.x, y = this.y) {
        this.move({ x: ++x, y })
    }

    previous(x = this.x, y = this.y) {
        this.move({ x: --x, y })
    }

    nextLine(x = this.x, y = this.y) {
        this.move({ x, y: ++y })
    }

    previousLine(x = this.x, y = this.y) {
        this.move({ x, y: --y })
    }

    move({ x, y }) {
        nextTick(() => {
            let el = document.getElementById(`x=${x}_y=${y}`)
            el && el.focus()
        })
    }
}

我这个是放在util文件里面的一个单独js文件, @/util/direction.js

接着我们就给el-table 设置一个按下事件

<el-table :data="xxx" border @keydown="keymove">
    <el-table-column></el-table-column> <!--里面的反正都是一个el-input就对了-->
</el-table>

然后呢给每一个el-input绑定成这样子的id,注意:如果你的el-table-column是循环的中间的index就改成你循环的那个index,如果是一个个手写的话就从0依次到结束 :id="'x=' +0+ '_y=' + $index"

<el-input
v-else :id="'x=' + index + '_y=' + $index"
v-model="row.xxx" 
clearable 
size="small"/>

接下来就直接在script里面声明一个方法,方法如下:

// 上下左右箭头移动
function keymove(e) {
    if (e.key.substring(0, 5) !== 'Arrow') return
    e.preventDefault()
    new DirectionKey(e.target.id, e.keyCode)
}

就没有然后啦,大概效果就是这样子

视频地址

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用Vue 3.0+Element-Plus开发后台管理系统的步骤: 1.首先,确保你已经安装了Node.js和npm包管理器。 2.使用以下命令安装Vue CLI 4: ```shell npm install -g @vue/cli ``` 3.使用Vue CLI创建一个新项目: ```shell vue create my-project ``` 4.在创建项目时,选择使用Vue 3.0版本,并启用class-style component语法: ```shell ? Please pick a preset: Manually select features ? Check the features needed for your project: Choose Vue version, Babel, Router, Vuex, CSS Pre-processors, Linter, Unit ? Choose a version of Vue.js that you want to start the project with 3.x (Preview) ? Use class-style component syntax? Yes ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? No ? Use history mode for router? (Requires proper server setup for index fallback in production) Yes ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass) ? Pick a linter / formatter config: ESLint with error prevention only ? Pick additional lint features: Lint on save ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files ``` 5.安装Element-Plus和Echarts 5.0: ```shell npm install element-plus echarts@5.0 ``` 6.在main.js中引入Element-Plus和Echarts: ```javascript import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' import echarts from 'echarts' const app = createApp(App) app.use(store) app.use(router) app.use(ElementPlus) app.config.globalProperties.$echarts = echarts app.mount('#app') ``` 7.现在你可以开始使用Element-Plus和Echarts来开发你的后台管理系统了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值