Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect keyboard navigation in angular material mat-selection-list

I want to make some actions When navigating between items using keyboard arrows.

Is there any event should i implemented? The selectionChange is fired by clicking ENTER but i want to activate the function while navigating by arrows.

<mat-selection-list #list (selectionChange)="selectionChange($event, list.selectedOptions)">
  <mat-list-option (mouseenter)="showEditIcon(true, i)" (mouseleave)="showEditIcon(false, i)"
                   *ngFor="let lotItem of lotList; let i = index;"
                   (click)="showLotDetails(lotItem, i)"
                   [value]='lotItem'>
like image 382
eladr Avatar asked Feb 03 '26 16:02

eladr


1 Answers

You can use the keydown keyboard event on your mat-selection-list in order to call your selectionChange() method. You'll need to add both (keydown.arrowup) and (keydown.arrowdown) event handlers.

<mat-selection-list #list 
       (selectionChange)="selectionChange($event, list.selectedOptions)"
       (keydown.arrowup)="selectionChange($event)" 
       (keydown.arrowdown)="selectionChange($event)">

    <mat-list-option (mouseenter)="showEditIcon(true, i)" (mouseleave)="showEditIcon(false, i)"
                   *ngFor="let lotItem of lotList; let i = index;"
                   (click)="showLotDetails(lotItem, i)"
                   [value]='lotItem'>

Here is a StackBlitz Demo.

like image 150
Faisal Avatar answered Feb 06 '26 06:02

Faisal