Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

html - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...