I would like to bind arrows keyup's events to jump to another input in a MatTable.
I would like to do something like the following situation:
mycomponent.html
<table mat-table [dataSource]="dataSource">
<ng-container matColumnDef="name">
<th th mat-header-cell *matHeaderCellDef>Name</th>
<td mat-cell *matCellDef="let element">
<input type="text" (keyup.arrowup)="jumpToPreviousNameInput()" (keyup.arrowdown)="jumpToNextNameInput()" (keyup.arrowright)="jumpToAgeInput()"/>
</td>
</ng-container>
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef>Age</th>
<td mat-cell *matCellDef="let element">
<input type="number" min="0" (keyup.arrowup)="jumpToPreviousAgeInput()" (keyup.arrowdown)="jumpToNextAgeInput()" (keyup.arrowleft)="jumpToNameInput()" />
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns; let element" class="table-row" </tr>
</table>
Is this possible with Angular 9?
I have no idea about how can I get the next input element in Angular. In Vanilla JS I would do something like:
let myTable = document.querySelector('table')
let inputs = myTable.querySelectorAll('input')
inputs.forEach((input, index) => {
input.addEventListener('keydown', e => {
const key = e.key;
switch(key) {
case "ArrowLeft":
try { inputs[index-1].focus() } catch {}
break;
case "ArrowRight":
try { inputs[index + 1].focus() } catch { }
break;
case "ArrowUp":
try { inputs[index - 2].focus() } catch { }
break;
case "ArrowDown":
try { inputs[index + 2].focus() } catch { }
break;
}
})
})