简体   繁体   English

有条件地改变表格数据的颜色 Angular 2+

[英]Conditionally change color of table data Angular 2+

I want to be able to highlight a specific td if a certain condition is met.如果满足特定条件,我希望能够突出显示特定的 td。

<tr *ngFor="let prospect of deployment">
                    <td>{{prospect.campaignName}} | {{prospect.campaignId}}</td>
                    <td>{{prospect.dealerName}} | {{prospect.dealerId}}</td>                
                    <td class="group-added">{{prospect.addedDate | date : 'shortDate' }}</td>
                    <td class="group-added" style="text-align: right;">{{prospect.addedProspects | number}}</td>
                    <td class="group-processed">{{prospect.startedDate | date : 'shortDate' }}</td>
                    <td class="group-processed">{{prospect.stage}}</td>                
                    <td class="group-processed" style="text-align: right;">{{prospect.processedProspects | number}}</td>
                    <td class="group-processed" style="text-align: right;">{{prospect.emailsSent | number}}</td>
                    <td class="group-processed" style="text-align: right;">{{prospect.emailsError | number}}</td>
                    <td *ngIf="perms.has(perms.CREATIVE) && prospect.bundleId != 0 && prospect.bundleId != null" style="text-align: right;">
                        <a [href]="'bundles/' + prospect.bundleId + '/assets'">Creative</a>
                    </td>
                </tr>

I want to highlight the entire row in red where prospect.processedProspects is greater than 100 and the amount of sent emails for that prospect is less than 50%.我想用红色突出显示整行,其中 prospect.processedProspects 大于 100,并且为该潜在客户发送的电子邮件数量小于 50%。 I'm not sure if I can complete this using interpolation and class binding alone.我不确定是否可以单独使用插值和 class 绑定来完成此操作。

export class DeploymentSummaryScreen implements OnInit {

    loading: boolean;

    deployment: Deployment[];
    startDate: Date;
    endDate: Date;

Not sure if more of the.ts file is needed.不确定是否需要更多的 .ts 文件。

You can use [ngClass] for doing this and assign a class to table row. 您可以使用[ngClass]进行此操作,并将一个类分配给表行。 The NgClass directive allows you to set the CSS class dynamically for a DOM element. NgClass指令使您可以为DOM元素动态设置CSS类。 You need to write css for your css-class that serves your purpose: 您需要为符合您目的的css-class编写CSS:

<tr [ngClass]="{'font-red': prospect.processedProspects > 10" *ngFor="let prospect of deployment">
                    <td>{{prospect.campaignName}} | {{prospect.campaignId}}</td>
                    <td>{{prospect.dealerName}} | {{prospect.dealerId}}</td>                
                    <td class="group-added">{{prospect.addedDate | date : 'shortDate' }}</td>
                    <td class="group-added" style="text-align: right;">{{prospect.addedProspects | number}}</td>
                    <td class="group-processed">{{prospect.startedDate | date : 'shortDate' }}</td>
                    <td class="group-processed">{{prospect.stage}}</td>                
                    <td class="group-processed" style="text-align: right;">{{prospect.processedProspects | number}}</td>
                    <td class="group-processed" style="text-align: right;">{{prospect.emailsSent | number}}</td>
                    <td class="group-processed" style="text-align: right;">{{prospect.emailsError | number}}</td>
                    <td *ngIf="perms.has(perms.CREATIVE) && prospect.bundleId != 0 && prospect.bundleId != null" style="text-align: right;">
                        <a [href]="'bundles/' + prospect.bundleId + '/assets'">Creative</a>
                    </td>
                </tr>

in css: 在CSS中:

     .font-red { color: red; }

You can also use NgStyle directive which lets you set a given DOM elements style properties. 您还可以使用NgStyle指令,该指令可让您设置给定的DOM元素样式属性。 Ex: 例如:

  <tr [ngStyle]="{'background-color': prospect.processedProspects > 10 ? 'green' : 'red' }">
    ......
</<tr>

You can use *ngClass to apply a class conditionaly. 您可以使用*ngClass地应用类。

<tr *ngFor="let prospect of deployment"
    *ngClass="{specialHighlight: prospect.processedProspects > 10 && yourConditionOnEmails}">

and of course the specialHighlight class is to be defined somehow in your css. 当然, specialHighlight类将在您的CSS中定义。

You can use [ngClass] to achieve that.您可以使用 [ngClass] 来实现这一点。 Following is my code, which is tested and working fine.以下是我的代码,它已经过测试并且工作正常。 Following sample use Material Icon as an example.以下示例以 Material Icon 为例。 So you must include following line in the app.module.ts .因此,您必须在app.module.ts中包含以下行。

import { MatIconModule } from '@angular/material/icon';

Include following line in the index.html :index.html中包含以下行:

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

In your html file:在您的 html 文件中:

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

<!-- description Column -->
<ng-container matColumnDef="description">
  <th mat-header-cell *matHeaderCellDef> Description </th>
  <td mat-cell *matCellDef="let element"
  [ngClass]="{'specialHighlight': readIcon == 'mail'}"> {{element.description}} </td>
</ng-container>

<!-- trash Column -->
<ng-container matColumnDef="trash">
  <th mat-header-cell *matHeaderCellDef></th>
  
  <td mat-cell *matCellDef="let element">
    <span>
    <mat-icon aria-hidden="false" aria-label="Example home icon" (click)="changeRead()">{{ readIcon }}</mat-icon>
  </span>
  </td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"
    (click)="rowClicked(row)">
</tr>

In your ts file:在你的 ts 文件中:

/* readIcon indicates if the row (mail) is read */
readIcon = 'drafts';

changeRead() {
  if (this.readIcon == 'drafts') this.readIcon = 'mail';
  else this.readIcon = 'drafts';

} }

In your css file:在您的 css 文件中:

.specialHighlight {
  font-weight: bold;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM