Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 402
dafna Avatar asked Nov 20 '25 05:11

dafna


1 Answers

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

like image 160
Rahul Singh Avatar answered Nov 24 '25 01:11

Rahul Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!