Vuejs - 按下按键并单击表格行

时间:2016-09-01 14:17:23

标签: vue.js

如何捕获按键并单击tr元素?

我需要实现一个可以处理单行选择或多行选择的表。

现在,我尝试绑定密钥ctrl

Vue.directive('on').keyCodes.ctrl = 17;

但是,如果我使用@keyup.ctrl确定这不起作用,因为我需要检查当用户点击一行时按下了什么键。

1 个答案:

答案 0 :(得分:3)

点击事件包含指示在点击期间是否按下了Control,Shift,Alt或Meta键的属性。



new Vue({
  el: 'body',
  data: {
    controlled: false,
    shifted: false,
    meta: false,
    alted: false
  },
  methods: {
    clicked: function(event) {
      console.debug(event);
      this.controlled = event.ctrlKey;
      this.shifted = event.shiftKey;
      this.meta = event.metaKey;
      this.alted = event.altKey;
    }
  }
});

<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<button @click="clicked">Click me!</button>
<div v-if="controlled">Control was pressed</div>
<div v-if="shifted">Shift was pressed</div>
<div v-if="alted">Alt was pressed</div>
<div v-if="meta">Meta was pressed</div>
&#13;
&#13;
&#13;