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:
<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.
Can anyone help me?