Alternative row colors angular material table

I am wondering how I aim even / odd lines inside Angular Material Tableso that I can set even / odd lines of a different background color.

I am setting up a class ClaimFileHistoryDataSource:

claimClientInformation: ClaimFileHistory[];
dataSource : ClaimFileHistoryDataSource;
displayedColumns = ['TypeImg', 'Description', 'Agent'];


export class ClaimFileHistoryDataSource extends DataSource<ClaimFileHistory> {

    constructor(private _claimFileHistory: ClaimFileHistory[]) {
      super();
    }

    connect(): Observable<ClaimFileHistory[]> {
      return Observable.of(this._claimFileHistory);
    }

    disconnect() {}
}

As NgInitI call to their service to go and get me the necessary data and to fill in DataSource:

this._claimFileHistoryService.getClaimFileHistoryById().subscribe(response => {
  this.claimClientInformation = response;       
  this.dataSource = new ClaimFileHistoryDataSource(this.claimClientInformation);
});

This is normal and the data is returned properly.

In HTML Mat-Tableit looks like this:

    <mat-table #table [dataSource]="dataSource">

      <ng-container matColumnDef="TypeImg">
        <mat-cell *matCellDef="let row"><img [src]='row.TypeImg' height="40px"></mat-cell>
      </ng-container>

      <ng-container matColumnDef="Description">
        <mat-cell *matCellDef="let row">
          <div>
            <span class="d-block">{{row.Description}}</span>
            <span class="d-block">{{row.Date}}</span>
          </div>

        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="Agent">
        <mat-cell *matCellDef="let row"> {{row.Agent}} </mat-cell>
      </ng-container>

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

Again I wondered, how do I deal with getting the rows of the Odd / Even table and setting them to a different row background color?

+17
source share
3 answers

Use Follow CSS in your .css or .scss file to set a different style for the even / odd line:

   .mat-row:nth-child(even){
          background-color:red;
          }

   .mat-row:nth-child(odd){
          background-color:black;
          }
+46

, .

Angular .

<mat-row *matRowDef="
              let row;
              let even = even; 
              columns: displayedColumns;" 
         [ngClass]="{gray: even}"></mat-row>

CSS : .gray { background-color: #f5f5f5 }

, : index, count, first, last, even odd.

Angular Docs, ", "

+5

You can also apply colors to strings depending on conditions.

For example, if the cell value is 100, change the row color to red.

     <tr class="matheader-row" mat-row *matRowDef="let row; columns: displayColumns; 
      let i = index; let even = even;" [class.rowStyle]="row['myColumn']=='100'"
                [ngClass]="{rowcolor: even}">
        </tr>

CSS

.rowStyle{
background-color:red;
font-weight: bold;
}

The above script will work if your columns have myColumn as one of the columns. Also using the even property, you can apply the required color style[ngClass]="{rowcolor: even}

0
source

Source: https://habr.com/ru/post/1688514/


All Articles