angular aggrid 触摸双击

  项目中,正常主列表行双击是有触发事件的,然而在 ipad 上的浏览器,双击事件不会被触发

  查了下相关信息,首先 aggrid 貌似是不支持 touch 相关的 api,而且 angular 底层的 dblclick 监听也触发不了

  所以思考了一下,模拟监听双击

  @HostListener 有一个监听 touch 的方法: touchstart

  首先测试了一下该方法,ipad 上是能实现触发的,并且点触几次就触发几次:

@HostListener('touchstart', ['$event'])
_touchstart() {
  console.log('touch start');
}

  双击间隔很短,那么就记录下单次点触的时间戳,第二次点触到来时,时间是否在双击时间限定范围内,这里预设 300ms,如果不满足,则更新点触时间戳

public preTouchTime: number;
@HostListener('touchstart', ['$event'])
_touchstart() {
  const current = new Date().getTime();
  if (this.preTouchTime && current - this.preTouchTime < 300) {
    console.log('双击');
  } else {
    this.preTouchTime = current;
  }
}

  双击监听实现了,现在需要知道双击在哪,判断 $event 的 target 太麻烦了,直接就用 aggrid 的 rowClick 事件,也记录一个时间戳,如果当次双击的时间范围中,存在点击行的时间戳,那么,说名双击的行~(这里log 了好几遍,确定了触发顺序,touchstart 先触发,之后是 aggrid 的 rowClick 行点击事件)

public preRowTouchTime: number;
@HostListener('touchstart', ['$event']) _touchstart() { const current = new Date().getTime(); if (this.preTouchTime && current - this.preTouchTime < 300) { console.log('双击'); if (this.preTouchTime < this.preRowTouchTime && this.preRowTouchTime < current) { console.log('行双击'); } } else { this.preTouchTime = current; } }

  点触版双击搞定!

原文地址:https://www.cnblogs.com/guofan/p/15149050.html