Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight angular-material table row conditionally?

I just started with a basic approach so I want to highlight row color conditionally its not throwing any error but does not apply the background-color to the row. I have 5 rows that have riskINdex H , Any idea what is implemented wrong in below code?

app.component.html

<div>
 <mat-table>
  <ng-container matColumnDef="eventType">
        <mat-header-cell *matHeaderCellDef> Event Type </mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element.eventType}} </mat-cell>
      </ng-container>
      <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
      <mat-row *matRowDef="let row; columns: displayedColumns;" (click)="selectedRow(row)" [ngClass]="{ 'high': row.riskIndex === 'High'}"></mat-row>
    </mat-table>
</div>

component.css

.high {
  background-color: red;
}
like image 404
hussain Avatar asked Dec 05 '17 20:12

hussain


Video Answer


1 Answers

Suppose, You want to highlight mat-row by some model property. lets say highlight when status == Approved.

<tr mat-row *matRowDef="let row; columns: displayedColumns;let entry"[ngClass]="{ 'highlight': entry.status == 'Approved' }"></tr>

In the above line of code,

highlight is css class defined in .css file.

.highlight{
    background: #42A948; /* green */
  }

The above .css file included in component as following.

 @Component({

      styleUrls: ['./expense.component.css'],

    })
like image 78
immirza Avatar answered Oct 27 '22 14:10

immirza