el-table 实现鼠标上下移动

源码贴在这里已经成功实现,给大家一个比较好的思路。

<template>
   <div id="hdapp">
      <template>
        <el-table
          :data="tableData"
          style="width: 100%"
          highlight-current-row
          ref="singleTable"
        >
          <el-table-column prop="date" label="日期" width="180">
            <template slot-scope="scope">
              <el-input
                :ref="`input${scope.$index}`"
                :value="scope.row.date"
                :focus="true"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="address" label="地址"> </el-table-column>
        </el-table>
      </template>
    </div>
</template>

<script>

export default {
  
 data() {
    return {
      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 弄',
        },
      ],
      index: 1, //改变这个值 实现对应行高亮 聚焦
    }
  },
      mounted() {
        this.$nextTick(()=>{
          this.$refs.singleTable.setCurrentRow(this.tableData[0])
        })
        // window.addEventListener('keyup', (e)=>{ 
        //   this.cursorup()
        //   console.log(e,'上去') 
        // })

        window.addEventListener('keydown', (e)=>{ 
          let code = e.keyCode
          if(code==40) {
            this.cursordown()
          }
          if(code==38) {
            this.cursorup()
          }
          console.log(e,'下来') 
        })

      },
      methods: {

         cursorup() {
           let len = this.tableData.length-1
           this.index -= 1;
           if (this.index === -1) {
             this.index = len
           }
          this.setHighRow()
        },  

      cursordown() {
           let len = this.tableData.length
           this.index += 1;
           if (this.index === len) {
             this.index = 0
           }
          this.setHighRow()
        },  
        setHighRow() {
          const row = this.tableData[this.index];
          this.$refs.singleTable.setCurrentRow(row);
          this.$nextTick(() => {
            const refName = `input${this.index}`;
            this.$refs[refName].focus();
          });
        },
      },
}
</script>

<style>

</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值