Vue:Element组件el-input输入框支持上下左右回车键跳转

页面大概长这样

<el-table :data="items" style="width: 100%" max-height="240" class="m-t-10">
  <el-table-column prop="lineNo" label="序号" width="50">
    <template slot-scope="scope">
      {{ scope.$index + 1 }}
    </template>
  </el-table-column>
  <el-table-column prop="gName" label="商品名称" />
  <el-table-column label="箱数">
    <template slot-scope="scope">
      <div v-if="form.goods[scope.$index]">
        <el-input
          :ref="'cartonQty'+scope.$index"
          v-model="form.goods[scope.$index].cartonQty"
          @keyup.native="moveFocus($event, scope.$index, 'cartonQty')"
        />
      </div>
    </template>
  </el-table-column>
  <el-table-column prop="grossWeight" label="毛重">
    <template slot-scope="scope">
      <div v-if="form.goods[scope.$index]">
        <el-input
          :ref="'grossWeight'+scope.$index"
          v-model="form.goods[scope.$index].grossWeight"
          @keyup.native="moveFocus($event, scope.$index, 'grossWeight')"
        />
      </div>
    </template>
  </el-table-column>
  <el-table-column prop="netWeight" label="净重">
    <template slot-scope="scope">
      <div v-if="form.goods[scope.$index]">
        <el-input
          :ref="'netWeight'+scope.$index"
          v-model="form.goods[scope.$index].netWeight"
          @keyup.native="moveFocus($event, scope.$index, 'netWeight')"
        />
      </div>
    </template>
  </el-table-column>
</el-table>

实现的js代码

moveFocus(event, index, key) {
  console.log(key, index)

  // cartonQty0 grossWeight0 netWeight0
  // cartonQty1 grossWeight1 netWeight1
  // cartonQty2 grossWeight2 netWeight2

  const keyfield = ['cartonQty', 'grossWeight', 'netWeight']

  if (event.keyCode === 13) { // 回车
    if (index === this.declItems.length - 1 && key === keyfield[keyfield.length - 1]) { // 最后一行最后一个
      return
    }

    this.$refs[key + index].blur()
    if (key === keyfield[keyfield.length - 1]) { // 当前行最后一个,跳转下一行第一个
      this.$refs[keyfield[0] + (index + 1)].focus()
    } else { // 跳转下一个
      const nextkeyindex = keyfield.findIndex(k => k === key) + 1
      this.$nextTick(() => {
        this.$refs[keyfield[nextkeyindex] + index].focus()
      })
    }
  }

  // 向上 =38
  if (event.keyCode === 38) {
    console.log('向上')
    if (index === 0) { // 第一行
      console.log('第一行无法向上')
      return
    }
    this.$refs[key + index].blur()
    this.$nextTick(() => {
      this.$refs[key + (index - 1)].focus()
    })
  }

  // 下 = 40
  if (event.keyCode === 40) {
    console.log('向下')
    if (index === this.declItems.length - 1) { // 最后一行
      console.log('最后一行无法向下')
      return
    }
    this.$refs[key + index].blur()
    this.$nextTick(() => {
      this.$refs[key + (index + 1)].focus()
    })
  }

  // 左 = 37
  if (event.keyCode === 37) {
    console.log('向左')
    if (index === 0 && key === keyfield[0]) { // 第一行第一个
      console.log('第一行第一个无法向左')
      return
    }
    this.$refs[key + index].blur()
    if (key === keyfield[0]) { // 当前行第一个,跳转上一行最后一个
      this.$refs[keyfield[keyfield.length - 1] + (index - 1)].focus()
    } else { // 跳转上一个
      const prevkeyindex = keyfield.findIndex(k => k === key) - 1
      this.$nextTick(() => {
        this.$refs[keyfield[prevkeyindex] + index].focus()
      })
    }
  }

  // 右 = 39
  if (event.keyCode === 39) {
    console.log('向右')
    if (index === this.declItems.length - 1 && key === keyfield[keyfield.length - 1]) { // 最后一行最后一个
      console.log('最后一行最后一个无法向左')
      return
    }
    this.$refs[key + index].blur()
    if (key === keyfield[keyfield.length - 1]) { // 当前行最后一个,跳转下一行第一个
      this.$refs[keyfield[0] + (index + 1)].focus()
    } else { // 跳转下一个
      const nextkeyindex = keyfield.findIndex(k => k === key) + 1
      this.$nextTick(() => {
        this.$refs[keyfield[nextkeyindex] + index].focus()
      })
    }
  }
},

这样就完成了

 

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AGAN丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值