Update for Newer Material Version (md –> mat):
html:
<!-- Add the highlight class in row definiton of md-table -->
<!-- Add click event to pass the selected row index -->
<mat-row *cdkRowDef="let row; columns: displayedColumns;"
[ngClass]="{'highlight': selectedRowIndex == row.id}"
(click)="highlight(row)">
</mat-row>
Original Answer:
You can do it by using ngClass
and a flag like selectedRowIndex
. Whenever clicked row index is equal to selectedRowIndex
, the class will be applied.
html:
<!-- Add the highlight class in row definiton of md-table -->
<!-- Add click event to pass the selected row index -->
<md-row *cdkRowDef="let row; columns: displayedColumns;"
[ngClass]="{'highlight': selectedRowIndex == row.id}"
(click)="highlight(row)">
</md-row>
css:
.highlight{
background: #42A948; /* green */
}
ts:
selectedRowIndex = -1;
highlight(row){
this.selectedRowIndex = row.id;
}