[英]Set focus on input field after click in table column (Angular 7)
I have the problem, that I need the focus on the clicked column field in my table. 我有一个问题,我需要将重点放在表中的单击列字段上。 I always have to click two times on the field to modify the content in the input field.
我总是必须在该字段上单击两次以修改输入字段中的内容。
HTML: HTML:
<table mat-table [dataSource]="dataSource">
<ng-container matColumnDef="ID">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td mat-cell *matCellDef="let elem" (click)="editCell(elem.ID)" (keydown.enter)="editCell($event.keyCode)">
<span *ngIf="!editable"> {{elem.ID}} </span>
<mat-form-field *ngIf="editable">
<input matInput [(ngModel)]="elem.ID">
</mat-form-field>
</td>
</ng-container>
...
</table>
Component: 零件:
editCell(item: any): void {
console.log('item: ', item);
if(!this.editable) {
this.editable = !this.editable;
} else if(item === 13) {
this.editable = !this.editable;
}
}
I read in other threads something about the template reference variable
and the method .focus()
but it doesn't work for me. 我在其他线程中阅读了一些有关
template reference variable
和方法.focus()
但它对我不起作用。
Can anyone help me? 谁能帮我?
尝试这个
(focus)="editCell('focusin')" (focusout)="editCell('focus out')"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.