Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply some style to the table row in angular material table

I have this table :

    <div class="table-container">
    <table mat-table [dataSource]="dataSource">
        <mat-divider></mat-divider>

        <!-- title column -->
        <ng-container matColumnDef="title">
            <th mat-header-cell *matHeaderCellDef> {{ 'NOTIFYCATION.NOTIFY_TITLE' | translate }} </th>
            <td mat-cell *matCellDef="let element"> {{element.title}} </td>
        </ng-container>

        <!-- code column -->
        <ng-container matColumnDef="description">
            <th mat-header-cell *matHeaderCellDef> {{ 'NOTIFYCATION.DESCRIPTION'  | translate }} </th>
            <td mat-cell *matCellDef="let element"> {{element.description}} </td>
        </ng-container>
        <!-- code column -->
        <ng-container matColumnDef="receiverDisplayName">
            <th mat-header-cell *matHeaderCellDef> {{ 'NOTIFYCATION.RECEIVER_DISPLAY_NAME'| translate }} </th>
            <td mat-cell *matCellDef="let element"> {{element.receiverDisplayName}} </td>
        </ng-container>
        <!-- code column -->
        <ng-container matColumnDef="type">
            <th mat-header-cell *matHeaderCellDef> {{ 'NOTIFYCATION.TYPE'| translate }} </th>
            <td mat-cell *matCellDef="let element"> {{element.type}} </td>
        </ng-container>
        <ng-container matColumnDef="createdOnUtc">
            <th mat-header-cell *matHeaderCellDef> {{ 'USER_SUBSCRIBE.createdOnUtc' | translate }} </th>
            <td mat-cell *matCellDef="let element">
                <span *ngIf="lang=='fa'">{{ element.createdOnUtc | jalali }}</span>
                <span *ngIf="lang!='fa'"> {{element.createdOnUtc | date: 'dd/MM/yyyy hh:mm'}} </span>
            </td>
        </ng-container>

        <!-- actions -->
        <ng-container style="color: red;" matColumnDef="actions">
            <th mat-header-cell *matHeaderCellDef>
                {{ 'GENERAL.ACTIONS' | translate }}
            </th>
            <td mat-cell *matCellDef="let row; let i=index;">
                <a mat-icon-button [matTooltip]="'TOOLTIP.DETAIL' | translate">
                    <mat-icon aria-label="Show" (click)="showDetail(row)" class="ic-defualt">remove_red_eye
                    </mat-icon>
                </a>
                <button mat-icon-button [matTooltip]="'TOOLTIP.DELETE' | translate" color="accent" uaccess
                    [permission]="':GiftCode:Delete'" (click)="delete(row.id)">
                    <mat-icon aria-label="Delete">delete</mat-icon>
                </button>
            </td>
        </ng-container>



        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr *matCellDef="let row; let element" [ngClass]="{'highlight': element.isSeen ==='true'}">
            {{element.bestRider}} </tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
    <mat-progress-bar *ngIf="dataSource.loading$ | async" mode="indeterminate"></mat-progress-bar>
    <mat-paginator [length]="dataSource.length$ | async" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions"
        showFirstLastButtons></mat-paginator>

</div>

I need to change color of tr to red when isSeen=false .

I try this :

<tr *matCellDef="let row; let element" [ngClass]="{'highlight': element.isSeen ==='true'}"></tr>

CSS :

 .highlight{
    color: white;
    background: #673AB7;
  }

but it not worked.

Whats the problem? how can I solve this problem?

like image 351
kianoush dortaj Avatar asked Oct 14 '19 05:10

kianoush dortaj


2 Answers

Your last mat-header-row has missing some code and use row.isSeen which contains the current iteration row in it.

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" [ngClass]="{'highlight': row.isSeen }"></tr>

Replace last mat-header-row with above code and will work:

Here is the Online_Demo

like image 152
Prashant Pimpale Avatar answered Sep 28 '22 18:09

Prashant Pimpale


You will need to move your style to style.css.Also no need to add 'true' if it is a Boolean do it like

<tr *matCellDef="let row; let element" [ngClass]="{'highlight': element.isSeen }"></tr>

Or add ::ng-deep to your style if you want to keep it in your component

::ng-deep .highlight{
    color: white;
    background: #673AB7;
  }

demo

like image 43
jitender Avatar answered Sep 28 '22 17:09

jitender