源码贴在这里已经成功实现,给大家一个比较好的思路。
<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>