需求:
表格单击选中,双击显示修改,所以需要在行数据上面添加单击与双击。这时候就要区分开了。
环境:
angualr、ng-zerro
实现:
双击的时候肯定会先触发单击,所以我们进行单击拦截。这里拦截使用定时器拦截,如果触发双击则进行定时器清除。不然就实现单击逻辑。
创建单击及双击事件,如下所示:
<tr *ngFor="let data of rowSelectionTable.data" (click)="clickItem(data.columnId)" (dblclick)="dbClickItem(data)" >
首先声明标识符,用于判断单击是否正在执行,这里之前没有添加标识符,导致触发两次单击,定时任务被重新赋值,所以清除了一个定时任务,导致另一个正常执行依然触发单击。
可以理解为 单击为“单例”模式的单击事件。
isSingle=false;
timeout;
clickItem(id){
var that=this;
let _id = id;
if(!this.isSingle){
this.isSingle=true;
this.timeout = setTimeout(function () {
console.log('你单击');
that.isSingle = false;
that.mapOfCheckedId[_id]=!that.mapOfCheckedId[_id];
},250)
}
}
dbClickItem(e){
console.log('你双击');
this.isSingle = false;
console.log(this.timeout)
clearTimeout(this.timeout);
console.log(e);
}