dafna
dafna

Reputation: 973

Angular mat table - get click event working

I would like to select an item (row) of my mat table in my Angular app. Therefore I am using the Angular Material. I think I don't know the right place to add the (click)-event or do I have to do it in another way? Thanks for helping!

<div class="table-container mat-elevation-z8">
  <mat-table #table [dataSource]="dataSource" matSort>

    <ng-container matColumnDef="item_id">
      <mat-header-cell *matHeaderCellDef mat-sort-header> ID </mat-header-cell>
      <mat-cell *matCellDef="let item"> {{item.id}} </mat-cell>
    </ng-container>

    <ng-container matColumnDef="item_name">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell>
      <mat-cell *matCellDef="let item" (click)="onSelect(item)"> {{item.name}} </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>
</div>

The example is'nt working.

Upvotes: 3

Views: 12487

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 19622

Just Try like this

<ng-container matColumnDef="position">
  <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
  <mat-cell *matCellDef="let element" (click) = "click('aaa')" > {{element.position}} </mat-cell>
</ng-container>

Working Plunker link

Click on first columns of the table

Upvotes: 5

Related Questions