如何根据Mat-Dynamic-Table Angular中的json响应更改Mat-cell颜色?

问题描述 投票:0回答:1

我正在使用动态mat表来显示json响应。我想根据某些条件更改单元格颜色。但是,如果我尝试应用[ngStyle],则它适用于整行。 我只想将其应用于特定的单元格

  <mat-table #table [dataSource]="dataSource">
    <ng-container *ngFor="let column of columns" [cdkColumnDef]="column.columnDef">
      <mat-header-cell *cdkHeaderCellDef>{{ column.header }}</mat-header-cell>
      <mat-cell *cdkCellDef="let data" [ngStyle]="{'color': data.name == 'Boron' ? 'green':'red'}">{{ column.cell(data) }}</mat-cell>
    </ng-container>
    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>

任何人的帮助将不胜感激!!!

angular7 angular-material-table
1个回答
0
投票

您可以执行类似的操作,将ngStyle更改为

[ngStyle]="{'color': data.name == 'Boron' && column.header == "your table header name where you want this color" ? 'green':'red'}"

示例

enter image description here

在HTML中

 <ng-container *ngFor="let column of columnsDef">
<ng-container [matColumnDef]="column.id">
  <th mat-header-cell *matHeaderCellDef> {{column.id}} </th>
  <ng-container *ngIf="!column.dynamicCellComponent">
    <td mat-cell *matCellDef="let element"  [ngStyle]="{'color':column.id=='position' && element.position==1 ? 'red':''}" > 
      {{element[column.id]}}
    </td>
  </ng-container></ng-container></ng-container>  

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.