防止按键事件(f.e.enter-key)

Prevent Keypress-events ( f.e. enter-key)

我正在寻找来自 ui-grid 的停止键盘事件。例如:ui-grid 通过按回车键导航到下一行,这里我需要阻止这个动作。即使使用实现 ng-keypress-listener 的 cellTemplate,我也无法停止 ui-grid 通过按回车键切换到下一行。

有谁知道如何防止 ui-grid 或关闭键盘事件?

您可以通过覆盖它们的services/directives来调整默认功能。

我为你的案例创建了一个 Plunkr

复制包含用于 cellNavigation 的 keyListener 的整个服务并删除 ENTER-watcher。

getDirection: function (evt) {
      if (evt.keyCode === uiGridConstants.keymap.LEFT ||
        (evt.keyCode === uiGridConstants.keymap.TAB && evt.shiftKey)) {
        return uiGridCellNavConstants.direction.LEFT;
      }
      if (evt.keyCode === uiGridConstants.keymap.RIGHT ||
        evt.keyCode === uiGridConstants.keymap.TAB) {
        return uiGridCellNavConstants.direction.RIGHT;
      }

      /*
      if (evt.keyCode === uiGridConstants.keymap.UP ||
        (evt.keyCode === uiGridConstants.keymap.ENTER && evt.shiftKey) ) {
        return uiGridCellNavConstants.direction.UP;
      }
      */

      if (evt.keyCode === uiGridConstants.keymap.PG_UP){
        return uiGridCellNavConstants.direction.PG_UP;
      }

      /*
      if (evt.keyCode === uiGridConstants.keymap.DOWN ||
        evt.keyCode === uiGridConstants.keymap.ENTER && !(evt.ctrlKey || evt.altKey)) {
        return uiGridCellNavConstants.direction.DOWN;
      }
      */

      if (evt.keyCode === uiGridConstants.keymap.PG_DOWN){
        return uiGridCellNavConstants.direction.PG_DOWN;
      }

      return null;
},

direction.UPdirection.DOWN 检查被注释掉了,但其他一切都按预期工作。我在 Plunkr app.js.

的底部附加了服务

它很有魅力,非常感谢:)

我现在使用装饰器服务来覆盖 ui-grid 中的 getDirection 方法。

仅供参考。这是代码:

angular
.module('app')
.config(function($provide){
  $provide.decorator('uiGridCellNavService', function ($delegate, uiGridConstants, uiGridCellNavConstants) {
    var getDirection = $delegate.getDirection;
    $delegate.getDirection = function (evt) {
      if (evt.keyCode === uiGridConstants.keymap.LEFT ||
        (evt.keyCode === uiGridConstants.keymap.TAB && evt.shiftKey)) {
        return uiGridCellNavConstants.direction.LEFT;
      }
      if (evt.keyCode === uiGridConstants.keymap.RIGHT ||
        evt.keyCode === uiGridConstants.keymap.TAB) {
        return uiGridCellNavConstants.direction.RIGHT;
      }

      if (evt.keyCode === uiGridConstants.keymap.UP ||
        (evt.keyCode === uiGridConstants.keymap.ENTER && evt.shiftKey) ) {
        return uiGridCellNavConstants.direction.UP;
      }

      if (evt.keyCode === uiGridConstants.keymap.PG_UP){
        return uiGridCellNavConstants.direction.PG_UP;
      }
      // delete listener for key ENTER
      if (evt.keyCode === uiGridConstants.keymap.DOWN) {
        return uiGridCellNavConstants.direction.DOWN;
      }
      // original code
      /*if (evt.keyCode === uiGridConstants.keymap.DOWN ||
        evt.keyCode === uiGridConstants.keymap.ENTER) {
        return uiGridCellNavConstants.direction.DOWN;
      }*/

      if (evt.keyCode === uiGridConstants.keymap.PG_DOWN){
        return uiGridCellNavConstants.direction.PG_DOWN;
      }

      return null;
    };
    return $delegate;
  })