0

I have a mat table, in which I want to change the Background color of a particular cell conditionally.

My HTML:

   <ng-container matColumnDef="Status">
        <mat-header-cell *matHeaderCellDef mat-sort-header>
            Status
        </mat-header-cell>
        <mat-cell *matCellDef="let row"> {{row.Status}} </mat-cell>
    </ng-container>

I have the color value in the "row" variable with row.statusBgColor.

I tried with:

<ng-container matColumnDef="Status">
  <mat-header-cell *matHeaderCellDef mat-sort-header>
     Status
  </mat-header-cell>
  <mat-cell style="background-color: {{row.statusBgColor}}" *matCellDef="let row"> {{row.Status}}
  </mat-cell>
</ng-container>

But that didn't work. Can anyone help me out of this?

IAfanasov
  • 4,775
  • 3
  • 27
  • 42
angular_code
  • 319
  • 3
  • 18
  • 1
    [This post](https://stackoverflow.com/questions/49280975/angular-how-to-apply-ngstyle-conditions) will help you. – N.F. Jun 17 '21 at 05:36

2 Answers2

1

You can use ngStyle or style

Apply this property to mat-cell

  1. ngStyle

    [ngStyle]="{'background-color':row.statusBgColor}"

  2. style

    [style.backgroundColor]="row.statusBgColor"

khush
  • 2,702
  • 2
  • 16
  • 35
0

Got it .

                    <ng-container matColumnDef="Status">
                        <mat-header-cell *matHeaderCellDef mat-sort-header>
                           Status
                        </mat-header-cell>
                        <mat-cell [style.background-color]="row.statusBgColor" *matCellDef="let row"> {{row.Status}} </mat-cell>
                    </ng-container>
angular_code
  • 319
  • 3
  • 18