笔记:键盘控制table上下

309 阅读1分钟

image.png

image.png

监听键盘事件

 //监听键盘事件
 window.addEventListener("keyup", this.handleKeyup);

主要方法

//键盘控制
handleKeyup() {
    const e = event||window.event||arguments.callee.caller.arguments[0];
    //判断当前角标
    var selectindex = this.sampleList.findIndex((i) => {
       return this.selectSample.id == i.id;
    });
     // 上
    if (e && e.keyCode == 38 && selectindex > 0) {
    //用自己的
       this.$refs.singleTable.setCurrentRow(
         this.sampleList[selectindex - 1]
       );
        //防抖与节流
       this.times = setTimeout(() => {
       //处理逻辑
          clearTimeout(this.times);
       }, 300);
      }
    }
    // 下
    if (e && e.keyCode== 40 && selectindex < this.sampleList.length - 1){
     //用自己的
      this.$refs.singleTable.setCurrentRow(
            this.sampleList[selectindex + 1]
      );
      //防抖与节流
       this.times = setTimeout(() => {
       //处理逻辑
          clearTimeout(this.times);
       }, 300);
      }
}

关闭页面销毁

destroyed() {
    window.removeEventListener("keyup", this.handleKeyup);
},