javascript - Angular Focusout 事件无法与表一起正常工作
问题描述
我有一张桌子。每一行都有像和这样的html
元素。我想要做的是在我关注当前行之后触发一个事件()。但它不能正常工作,每当我关注当前行的任何列时它都会触发。为了解决事件,我创建了一个名为并使用如下的指令input
span
focusout
focus-out-directive
<tr *ngFor="let item of list" focus-out-directive>
您可以在此链接中看到我正在处理的完整代码
解决方案
如果将focusout
属性放在 a 上,它也会被子元素tr
触发。input
解决此问题的最佳方法是在指令中包含一个包含最后一个焦点单元格的行索引的属性。
event.target
给你当前input
元素,上面的两个父tr
元素是你可以用来获取当前行索引的元素。
export class FocusOutDirective {
@Output() onFocusOut: EventEmitter<boolean> = new EventEmitter<false>();
lastFocusedCellRowIndex: number = -1;
@HostListener("focusout", ["$event"])
public onListenerTriggered(event: any): void {
let currentFocusedRowIndex = event.target.parentNode.parentNode.rowIndex;
if(this.lastFocusedCellRowIndex != currentFocusedRowIndex){
this.lastFocusedCellRowIndex = currentFocusedRowIndex;
this.onFocusOut.emit(true);
console.log('focus out of row');
}
}
}
推荐阅读
- linq - linq external apply with like 语句
- python - 使用多个列表中的值创建数组
- reactjs - TypeError:dispatcher.useState 不是 React Native 的函数
- powershell - 术语“Get-MessageTrackingLog”未被识别为 cmdlet 的名称
- node.js - 内存NodeJS中的.DDS到.PNG转换
- php - Laravel,同一页面上的 2 个表单/ URL 发布到各个路由
- javascript - 为现有表的特定行添加类
- flutter - 有没有办法在最初显示表单时执行 TextFormField 上的 InputFormatters?
- python - 按小时分组,同时考虑夏令时
- c# - 将 texbox 中的数据保存在数组(向量)中