0
votes

Guys, I need to change my row Datatable color if a value of the object == 0, I know how to do that in normal cases but here I use programming row def ( I should use it for grouping )

So I've tried that code:

Template:

       <mat-table [dataSource]="dataSource" class="mat-elevation-z8">

            <ng-container *ngFor="let column of columns; let i = index" matColumnDef="{{ column.field }}">
              <mat-header-cell *matHeaderCellDef (click)="SortWith($event,column)">{{ column.field }}


              </mat-header-cell>
            <mat-cell *matCellDef="let row">
                <div *ngIf="{{row.RemainingQuantities}} == 25; else redBack">
                      <div class="red">
                        {{ row[column.field] }}
                      </div>
                </div>
                <ng-template #redBack>
                    <div>
                        {{ row[column.field] }}
                    </div>
                </ng-template>
            </mat-cell>
            </ng-container>

              <mat-header-row mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
              <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>

              <!-- Group header -->
              <ng-container matColumnDef="groupHeader">
                  <mat-cell colspan="999" *matCellDef="let group">
                <mat-icon *ngIf="group.expanded">expand_less</mat-icon>
                <mat-icon *ngIf="!group.expanded">expand_more</mat-icon>
                      <strong>{{groupByColumns[group.level-1]}} = {{group[groupByColumns[group.level-1]]}} ({{group.totalCounts}})</strong>
                </mat-cell>
            </ng-container>

            <mat-row *matRowDef="let row; columns: ['groupHeader']; when: isGroup" (click)="groupHeaderClick(row)"> </mat-row>

          </mat-table> 

My component.ts:

  this.columns = [{
      field: 'Category'
    },  {
      field: 'Model'
    },  {
      field: 'Reference'
    },  {
      field: 'Name'
    },  {
      field: 'RemainingQuantities'
    },  {
      field: 'Department.Name'
    },  {
      field: 'Supplier.Name'
    }];
    this.displayedColumns = this.columns.map(column => column.field);
    this.groupByColumns = ['Category'];
 columnsToDisplay: string[] = ['Category', 'Model', 'Reference', 'Name', 'RemainingQuantities', 'Department.Name', 'Supplier.Name'];
   SortedColumns: string[] = [];
    this.service.get_product().subscribe((result : any)=>{
      this.listdata = result;

      this.decompersed = decrypt(result);
      console.log(this.decompersed);
      this.ProductList = this.decompersed;
      this.dataSource.data = this.addGroups(this.ProductList, this.groupByColumns);
      this.dataSource.filterPredicate = this.customFilterPredicate.bind(this);
      this.dataSource.filter = performance.now().toString();

But I found that error:

Uncaught Error: Template parse errors: TypeError: Cannot read property 'toUpperCase' of undefined ("at-header-cell> ]*ngIf="{{row.RemainingQuantities}} == 25; else redBack"> "): Soo Guys I need to test if the row of field RemainingQuantities == 0 and change it color to red.

2

2 Answers

1
votes

Instead of *ngIf, you could use [class] :


<mat-cell *matCellDef="let row">
   <div [className]="row.RemainingQuantities == 25 ? 'red' : 'redBack'">
      {{ row[column.field] }}
   </div>
</mat-cell>
0
votes

if is a row, you can use also the mat-row. I use [style.background-color], but as Adrien say you can use [className] or [ngClass].

 <tr mat-row *matRowDef="let row; columns: displayedColumns;" 
      [style.background-color]="!row.RemainingQuantities?'red':null"></tr>