angular(^4)-监控表格按键行为的问题

  之前开发的时候,用的是angular4,用ngGrid的时候要求对表格中的按键进行监控,具体如下:

  1)上下左右,按方向移动位置(指编辑单元格);

  2)enter,移动到下一个可编辑单元格;

  3)一个数字输入框(cellEditorFramework)里双击空格键完成某个特定操作;

注:因为后续了解到更多的便捷方法,特此将以下说明更新; 

   解决办法:

  1、光标移动

// 光标移动
  skipNextCell = (params: any) => {
    const previousCell = params.previousCellDef;
      const suggestedNextCell = params.nextCellDef; switch (params.key) { case KEY_DOWN: var nextRowIndex = previousCell.rowIndex - 1; if (nextRowIndex < 0) { return null; } else { return { rowIndex: nextRowIndex, column: previousCell.column, floating: previousCell.floating }; } case KEY_UP: var nextRowIndex = previousCell.rowIndex + 1; var renderedRowCount = gridOptions.api.getModel().getRowCount(); if (nextRowIndex >= renderedRowCount) { return null; } else { return { rowIndex: nextRowIndex, column: previousCell.column, floating: previousCell.floating }; } case KEY_LEFT: case KEY_RIGHT: return suggestedNextCell; default: throw "this will never happen, navigation is always on of the 4 keys above"; }
 }

 

  针对以上方法可以自定义上下左右键的行为;

  2、事件监听

  

@HostListener('keydown', ['$event'])
  onkeydown($event) {
    if (this.allowKeyboardEvent) {
      const focusedCell = this.detailListOptions.api.getFocusedCell();
      if ($event.keyCode === Keycode.ENTER && this.detailListOptions.api && this.detailListOptions.api.getFocusedCell()) {
        // 把焦点移动到下一个可编辑单元格
        this.focusNextEditableCell();
      } else if (this.rowIndex > 0 && ($event.which === 190 || $event.which === 191) && focusedCell && focusedCell['column']['colId'] === 'remark') {
        if (this.timer > 1) {
          this.abstractKeyListen($event.which);
        }
        this.timer += 1;
        const that = this;
        setTimeout(function() {
          that.timer = 1;
        }, 300);
      }
    }
  }

hostListener可以用来设置快捷键;

 

转载于:https://www.cnblogs.com/skylen/p/9242535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值