Hello I have an Angular application that displays in real time the status of processes running in a scheduler engine. I have the following table to display those processes:
What I want is to dynamically change the row background color depending on the Status, like following:
S (started) - White;
W (working) - Blue;
E (error) - Red;
F (finished) - Green;
Html
<table mat-table [dataSource]="this.dataSource" multiTemplateDataRows class="mat-elevation-z8" >
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
<div class="example-element-detail" [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'" *ngIf="log_message">
<div class="example-element-description">
<p *ngIf="this.log_message">Log Message:</p>
<p *ngIf="!this.log_message">N/A</p>
<pre *ngIf="this.log_message"> {{ this.log_message }} </pre>
</div>
</div>
</td>
</ng-container>
<ng-container matColumnDef="UPD_DATE">
<th mat-header-cell *matHeaderCellDef> UPD_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.LAST_UPD_DATETIME}} </td>
</ng-container>
<ng-container matColumnDef="S">
<th mat-header-cell *matHeaderCellDef> S </th>
<td mat-cell *matCellDef="let data"> {{data.STATUS}} </td>
</ng-container>
<ng-container matColumnDef="PROC">
<th mat-header-cell *matHeaderCellDef> PROC </th>
<td mat-cell *matCellDef="let data"> {{data.PROC_ID}} </td>
</ng-container>
<ng-container matColumnDef="AGENT">
<th mat-header-cell *matHeaderCellDef> AGENT </th>
<td mat-cell *matCellDef="let data"> {{data.AGENT_ID}} </td>
</ng-container>
<ng-container matColumnDef="SUBMIT_DATE">
<th mat-header-cell *matHeaderCellDef> SUBMIT_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.SUBMIT_DATE}} </td>
</ng-container>
<ng-container matColumnDef="END_DATE">
<th mat-header-cell *matHeaderCellDef> END_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.END_DATE}} </td>
</ng-container>
<ng-container matColumnDef="START_DATE">
<th mat-header-cell *matHeaderCellDef> START_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.START_DATE}} </td>
</ng-container>
<ng-container matColumnDef="USER_SUBMIT">
<th mat-header-cell *matHeaderCellDef> USER_SUBMIT </th>
<td mat-cell *matCellDef="let data"> {{data.USER_SUBMIT}} </td>
</ng-container>
<ng-container matColumnDef="THREAD_NUM">
<th mat-header-cell *matHeaderCellDef> THREAD_NUM </th>
<td mat-cell *matCellDef="let data"> {{data.THREAD_NUM}} </td>
</ng-container>
<ng-container matColumnDef="INSTANCE_ID">
<th mat-header-cell *matHeaderCellDef> INSTANCE_ID </th>
<td mat-cell *matCellDef="let data"> {{data.INSTANCE_ID}} </td>
</ng-container>
<ng-container matColumnDef="START">
<th mat-header-cell *matHeaderCellDef> START </th>
<td mat-cell *matCellDef="let row"><button class="btn btn-success" (click)="onStart(row)" [disabled]="this.started">Start</button></td>
</ng-container>
<ng-container matColumnDef="STOP">
<th mat-header-cell *matHeaderCellDef> STOP </th>
<td mat-cell *matCellDef="let row"><button class="btn btn-danger" (click)="onStop()">Stop</button></td>
</ng-container>
<ng-container matColumnDef="LOG">
<th mat-header-cell *matHeaderCellDef> LOG </th>
<td mat-cell *matCellDef="let row"><button class="btn btn-info" (click)="onLog()">Log</button></td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let element; columns: displayedColumns;" class="example-element-row"
[class.example-expanded-row]="expandedElement === element" (click)="log_message = onElementExpand(element); expandedElement = element;"></tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>
<mat-toolbar>
<mat-toolbar-row>
<button class="btn btn-primary" (click)="getIpeProcessInstances()" [disabled]="false">{{ 'button.Refresh' | translate }}</button>
<span class="example-spacer"></span>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</mat-toolbar-row>
</mat-toolbar>
CSS
table {
width: 100%;
}
tr.example-detail-row {
height: 0;
}
tr.example-element-row:not(.example-expanded-row):hover {
background: #f5f5f5;
}
tr.example-element-row:not(.example-expanded-row):active {
background: #efefef;
}
.example-element-row td {
border-bottom-width: 0;
}
.example-element-detail {
overflow: hidden;
display: flex;
}
.example-element-diagram {
min-width: 80px;
border: 2px solid black;
padding: 8px;
font-weight: lighter;
margin: 8px 0;
height: 104px;
}
.example-element-symbol {
font-weight: bold;
font-size: 40px;
line-height: normal;
}
.example-element-description {
padding: 16px;
}
.example-element-description-attribution {
opacity: 0.5;
}
.mat-elevation-z8 {
width: 100%;
}
.mat-row:hover {
background-color: whitesmoke;
}
.example-icon {
padding: 0 14px;
}
.example-spacer {
flex: 1 1 auto;
}
.mat-paginator
{
background-color: whitesmoke;
}
So based on the Status filed, how I can dinamicly change the row color? Thank you!
You can also use
[ngClass]
to achieve this:And then in your css:
Et voilà ! Hope it helps to understand that you can achieve this with different solution.
EDIT
To your use, juste use the
[ngClass]
in the second<tr>
Add the attribute with the value of a status and then write CSS for that attribute
e.g: HTML
So when view will get rendered your HTML will look like this:
Now add CSS for the selector:
let me know if you are still facing the issue. And possibly share the code next time through snippet.
Cheers :)
You can achieve this by using
[ngStyle]
onand my answer is not perfect as in this case you need to repeat
[ngStyle]
on everytd