Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material Table - Apply dynamically background color to a row (Angular 2+)

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: Table

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!

like image 512
Kiril1512 Avatar asked Nov 27 '18 09:11

Kiril1512


3 Answers

You can also use [ngClass] to achieve this:

<td [ngClass]="{
  'is-white': data.STATUS === 'S',
  'is-blue': data.STATUS === 'W',
  'is-red': data.STATUS === 'E',
  'is-green': data.STATUS === 'F'
}">...</td>

And then in your css:

td.is-white {
    background: ...;
}

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>

<tr mat-row *matRowDef="let element; columns: displayedColumns;" class="example-element-row"
  [class.example-expanded-row]="expandedElement === element"
  [ngClass]="{
  'is-white': element.STATUS === 'S',
  'is-blue': element.STATUS === 'W',
  'is-red': element.STATUS === 'E',
  'is-green': element.STATUS === 'F'
}" (click)="log_message = onElementExpand(element); expandedElement = element;"></tr>
like image 110
B.Benjo Avatar answered Nov 01 '22 18:11

B.Benjo


You can achieve this by using [ngStyle] on

<td mat-cell *matCellDef="let data" [ngStyle]="{'background-color': data.STATUS == 's' ? 'white': data.STATUS == 'w' ? 'blue' : data.STATUS == 'e' ? 'Red' : data.STATUS == 'f' ? 'green' : ''}"> {{data.STATUS}} </td>

and my answer is not perfect as in this case you need to repeat [ngStyle] on every td

like image 6
Farhat Zaman Avatar answered Nov 01 '22 19:11

Farhat Zaman


Add the attribute with the value of a status and then write CSS for that attribute

e.g: HTML

<tr [attr.row_status]="data.STATUS" /></tr>

So when view will get rendered your HTML will look like this:

<tr row_status="S"></tr>

Now add CSS for the selector:

[row_status="S"] {
   background: #fff
}

let me know if you are still facing the issue. And possibly share the code next time through snippet.

Cheers :)

like image 2
Mohit Mishra Avatar answered Nov 01 '22 19:11

Mohit Mishra