问题
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?
回答1:
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
回答2:
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
回答3:
Try this:
<mat-row *matRowDef="let row; columns: displayedColumns;" [ngClass]="{'highlight': row.IsTrue }">
and CSS:
.make-gold {
    background-color: gold
}
Example:
https://stackblitz.com/edit/angular-mat-highlight-row-on-conditions
回答4:
I think this would work perfectly.
.highlight {
    color: white;
    background: #673AB7;
}
.notHighlight {
    color: red;
    background: #252525;
}
HTML:
[ngClass]="(element.isSeen === true) ? 'highlight' : 'notHighlight'"
OR
[ngClass]="(element.isSeen) ? 'highlight' : 'notHighlight'"
回答5:
You can try this code:
HTML:
<tr *matCellDef="let row; let element" [ngClass]="{'highlight': element.isSeen ==='true'}"></tr>
CSS:
.highlight {
    background-color: gold
}
I hope it will helps you!
来源:https://stackoverflow.com/questions/58370523/how-to-apply-some-style-to-the-table-row-in-angular-material-table