功能介绍

vue方向键插件,适合键盘的快捷键操作,通过键盘在input间切换,应用在后台系统开单,财务等等的快速输入和保存上,使用简单,配置方便

使用方法

  • 安装
    npm install --save vue-direction-key
  • 使用
    在入口文件中引用
import Direction from 'vue-direction-key'
Vue.use(Direction)

在模版文件中使用
template中

<el-input placeholder="请输入内容" v-direction="{x: 0, y: 0}"></el-input>
<input type="text" v-direction="{x: 1, y: 0}">

script中

created: function () {let direction = this.$getDirection()direction.on('keyup', function (e, val) {if (e.keyCode === 39) {direction.next()}if (e.keyCode === 37) {direction.previous()}if (e.keyCode === 38) {direction.previousLine()}if (e.keyCode === 40) {direction.nextLine()}})
}

说明: x,y分别为x轴和y轴的坐标,事件的绑定必须在mounted之前,可以放在created中或者beforeMounted中(在mounted中绑定由于组建的渲染顺序可能会无效)

api

在vue组件钩子中使用this.$getDirection()获取direction对象,该对象有以下方法

  • direction.on(keys, fun)

    • 参数: keys: (string) 原生的事件参数如: 'keyup', 'keydown'等
      fun: 自定义函数,有两个参数function(e, val),e为event对象,val为触发的input绑定的自定义指令的值,可以通过此选项来传值进行特殊判断,例如:

    template中

    <input type="text" v-direction="{x: 1, y: 0, type: 'name'}">
    

    script中

    direction.on('keyup', function (e, val) {if (val.type === 'name') {console.log(111)}
    })
    
    • 作用: 给directive作用的原生input绑定事件,注意是绑定在原生的input上,例如:
    direction.on('keyup', function (e, val) {if (e.keyCode === 39) {direction.next()}if (e.keyCode === 37) {direction.previous()}if (e.keyCode === 38) {direction.previousLine()}if (e.keyCode === 40) {direction.nextLine()}
    })
    
  • direction.next(x, y)

    • 参数: x,y轴的坐标,例如(1, 1) <选填>,默认为当前focus的input坐标
    • 作用: 光标移动到下一个input
  • direction.previous(x, y)

    • 参数: x,y轴的坐标,例如(1, 1) <选填>,默认为当前focus的input坐标
    • 作用: 光标移动到上一个input
  • direction.previousLine(x, y)

    • 参数: x,y轴的坐标,例如(1, 1) <选填>,默认为当前focus的input坐标
    • 作用: 光标移动到上一行的input
  • direction.nextLine(x, y)

    • 参数: x,y轴的坐标,例如(1, 1) <选填>,默认为当前focus的input坐标
    • 作用: 光标移动到下一行的input
  • direction.onEnd

    • 作用: 函数,光标移动到最后一个input出发的函数
      例如:
    direction.onEnd = function () {console.log(111)
    }
    

在element表格中使用

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>vue-direction-key</title><script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script><link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"><!-- 引入组件库 --><script src="https://unpkg.com/element-ui/lib/index.js"></script><!-- 引入vue-direction-key --><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue-direction-key/direction.js"></script>
</head><body><div id="app"><el-table:data="tableData"style="width: 100%"><el-table-columnprop="date"label="日期"width="180"><template slot-scope="scope"><el-input v-model="scope.row.date" placeholder="请输入内容" v-direction="{x: 0, y: scope.$index}"></el-input></template></el-table-column><el-table-columnprop="name"label="姓名"width="180"><template slot-scope="scope"><el-input v-model="scope.row.name" placeholder="请输入内容" v-direction="{x: 1, y: scope.$index}"></el-input></template></el-table-column><el-table-columnprop="address"label="地址"><template slot-scope="scope"><el-input v-model="scope.row.address" placeholder="请输入内容" v-direction="{x: 2, y: scope.$index}"></el-input></template></el-table-column></el-table></div>
</body><script type="text/javascript">Vue.use(Direction)var app = new Vue({el: '#app',data: {tableData: [{date: '2016-05-02',name: '王小虎',address: '上海市普陀区金沙江路 1518 弄'}, {date: '2016-05-04',name: '王小虎',address: '上海市普陀区金沙江路 1517 弄'}, {date: '2016-05-01',name: '王小虎',address: '上海市普陀区金沙江路 1519 弄'}, {date: '2016-05-03',name: '王小虎',address: '上海市普陀区金沙江路 1516 弄'}]},created: function () {let direction = this.$getDirection()direction.on('keyup', function (e, val) {console.log(val)if (e.keyCode == 39) {direction.next()}if (e.keyCode == 37) {direction.previous()}if (e.keyCode == 38) {direction.previousLine()}if (e.keyCode == 40) {direction.nextLine()}})}})
</script>
</html>

多个组件共存

如果一个组件里面有多个表格或控件,可以支持自定义指令的参数,例如

<input type="text" v-direction:a="{x: 0, y: 0}">
<input type="text" v-direction:a="{x: 1, y: 0}">
<input type="text" v-direction:b="{x: 0, y: 0}">
<input type="text" v-direction:b="{x: 1, y: 0}">
created: function () {let a = this.$getDirection('a')a.on('keyup', function (e, val) {if (e.keyCode === 39) {a.next()}if (e.keyCode === 37) {a.previous()}if (e.keyCode === 38) {a.previousLine()}if (e.keyCode === 40) {a.nextLine()}})let b = this.$getDirection('b')b.on('keyup', function (e, val) {if (e.keyCode === 39) {b.next()}if (e.keyCode === 37) {b.previous()}if (e.keyCode === 38) {b.previousLine()}if (e.keyCode === 40) {b.nextLine()}})
}

原链接:https://www.jianshu.com/p/b12dbb0b17f9

vue如何通过键盘方向键切换input焦点相关推荐

  1. Vue使用虚拟键盘+中英文切换

    1.安装依赖 npm install simple-keyboard --save npm install simple-keyboard-layouts --save 2.虚拟键盘组件 simple ...

  2. Mac下Excel中无法用键盘方向键切换单元格

    原创simmy2012-02-16 16:02:05评论(0)566人阅读 同事在Mac下的Excel表要切换单元格,一般来说通过键盘即可,但是却发现单元格无法切换反而整个表会跟着移动.google后 ...

  3. 菜鸟修炼笔记--QT--【问题】界面切换时焦点各种异常的处理

    菜鸟修炼笔记--QT--[问题]界面切换时焦点各种异常的处理 前言 切换界面时需要完成的操作 1. 焦点的设置 2. 键盘的切换 其他焦点丢失的原因 前言 由于工作需要,我最近一直在使用QT来设计界面 ...

  4. qt类似电视盒子的通过方向键切换焦点的实现方法

    比如这样的需求需要焦点通过上下左右在按钮和widget或者其他窗口类中切换. 核心实现方法就是通过一个信号单例接收全局的键盘事件并发出信号每个要切换的焦点类都连接上接收到信号的窗口类或者button将 ...

  5. jquery监听pda 按键_js 点击input焦点不弹出键盘 PDA扫描枪

    直接贴代码 1.利用input readonly属性 当input有readonly属性的时候,即使获取焦点,也不会吊起小键盘 扫码枪输入的间隔大概在15-60毫秒,然后手动输入的100-200毫秒之 ...

  6. js 点击input焦点不弹出键盘 PDA扫描枪直接贴代码

    1.利用input readonly属性 当input有readonly属性的时候,即使获取焦点,也不会吊起小键盘 扫码枪输入的间隔大概在15-60毫秒,然后手动输入的100-200毫秒之间 onfo ...

  7. Vue 自定义指令 解决IOS webview input 获取焦点被键盘遮挡

    Vue 自定义指令 解决IOS webview input 获取焦点被键盘遮挡 创建自定义指令 在使用input的地方添加自定义指令,记录一下还有优化空间. vue 文件 <div id=&qu ...

  8. vue获取input的属性_Vue中自动获取input焦点

    1.给input属性添加autofocus属性,缺点autofocus 在移动版 Safari 上不工作 2.Vue官网给出的解决办法 // 注册一个全局自定义指令 `v-focus` Vue.dir ...

  9. vue实现1-4-9宫格切换

    vue实现1-4-9宫格切换 <!DOCTYPE html> <html lang="en"><head><meta charset=&q ...

最新文章

  1. 又是华为!对标 TensorFlow、PyTorch,深度学习框架 MindSpore已开源!附入手公开课...
  2. C# 托管资源和非托管资源
  3. 深度学习未来十大趋势
  4. 一道有意思的数据库题
  5. github 修改项目为public_在GitHub上为开源项目做贡献
  6. 【TensorFlow-windows】扩展层之STN
  7. 全面了解HTTP和HTTPS(开发人员必备)
  8. 吉大计算机学院刘淑芬,刘淑芬-吉林大学计算机科学与技术学院
  9. Oracle 11g Dataguard搭建及知识梳理
  10. python虚拟cpu性能_基于Tensorflow:CPU性能分析
  11. 解决XP的IIS HTTP 500”内部服务器错误
  12. C#生成CHM文件(应用篇)之代码库编辑器(1)
  13. 数据库、SQL脚本、存储过程执行准则(*****)
  14. oracle-j2sdk1.8,cloudera-manager – 没有包oracle-j2sdk1.7可用?
  15. mac 修改hosts 文件的方法
  16. 电脑远程连接已停止工作 解决方案
  17. 国际贸易和计算机网络,网络对国际贸易的变革与影响.doc
  18. arduino继电器控制风扇_Arduino 笔记 - Lab21 使用继电器控制12V风扇
  19. PS制作科幻特效的金色立体文字
  20. Android初学者需掌握的几点经验:该如何自学Android开发?(Android自学资料大全)

热门文章

  1. android SoundPool 音效播放
  2. 微信小程序(uni-app)
  3. 百度地图SDK for Android【覆盖物】
  4. 卡尔曼滤波器之经典卡尔曼滤波
  5. 多雷达视频融合(1)——需求分析及解决方案
  6. [Android]判断滑动是向左还是向右?
  7. Win11家庭版无法远程连接的解决办法
  8. 注会会计-会计账户与记账方法
  9. 计算机专业顶岗实训,计算机专业学生的顶岗实习
  10. 现代轻奢风格装修讲解