3

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.

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! That was fast! The alert is working in my app, too. But not my other function. But then there must be another problem. I try to solve it in my own. Thanks!
But how can I change the mouse icon from arrow to the hand. Do you know?
@dafna change css cursor for the element to this { cursor: pointer; }
Working! Thanks! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.