how to add a secondary action in mat-list-option (mat-selection-list) item
Asked Answered
G

2

9

How is it possible, to add an secondary action (mat-icon-button) in an mat-selection-list inside the mat-list-option element.

Currently all items are added inside the mat-list-text div.

example (https://stackblitz.com/edit/angular-dwac7y):

<mat-selection-list [(ngModel)]="selectedOptions">
    <mat-list-option *ngFor="let item of items;" [value]="item.id" [disableRipple]="true">
        <mat-icon matListAvatar>person</mat-icon>

        <h3 matLine> {{item.name}}</h3>
        <p matLine>
            {{item.details}}
        </p>

        <button mat-icon-button (click)="onEdit($event, item)">
            <mat-icon>edit</mat-icon>
        </button>
    </mat-list-option>
</mat-selection-list>
Gilded answered 17/12, 2018 at 12:45 Comment(2)
sorry for the late answer, was busy. thanks for the stackblitz. im still strugling to understand what's your goal. Do you just want to move the "edit-icon-button" beside the checkboxes?Hunt
Hi Cris, yes exactly. Like the example with the nav-list.Gilded
H
8

you could do it as following.

<h2>Selection-List with secondary action:</h2>

<mat-selection-list [(ngModel)]="selectedOptions">
  <mat-list-option *ngFor="let item of items;" [value]="item.id" [disableRipple]="true">
    <div style="display:flex; justify-content:space-between;align-items:center">
      <div style="display:flex;align-items:center">
        <mat-icon matListAvatar>person</mat-icon>

        <div style="display:flex; flex-direction:column">
          <div>
            <h3 matLine> {{item.name}}</h3>
          </div>
          <div style="margin-top:-30px">
            <p matLine>
              {{item.details}}
            </p>
          </div>
        </div>
      </div>
      <div>
        <button mat-icon-button (click)="onEdit($event, item)">
          <mat-icon>edit</mat-icon>
        </button>
      </div>
    </div>
  </mat-list-option>
</mat-selection-list>

but the following style...

<div style="margin-top:-30px">

is not a good solution imo.

enter image description here

Hunt answered 18/12, 2018 at 10:31 Comment(0)
T
2

I think the event should look like this.

onEdit(event, item){
    event.stopPropagation();
} 
Triumvir answered 11/7, 2020 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.