我可以帮忙翻译。这段内容是关于如何使用 CSS 样式来为 Angular Material 的 mat-table 添加交替行颜色的。以下是 CSS 样式代码:
这在我的mat-table没有定义子行时有效。当我添加了另一个指定为“expandedDetail”的
我按照material.angular.io的示例进行操作。
我看到其他关于行样式的示例,但它们使用tr标签。在示例和我的代码中,我为每一列使用了
任何帮助都将不胜感激。
我已将项目添加到stackblitz。
.mat-row:nth-child(even){
background-color: #e4f0ec;
}
.mat-row:nth-child(odd){
background-color:#ffffff;
}
这在我的mat-table没有定义子行时有效。当我添加了另一个指定为“expandedDetail”的
ng-container
时,这就不再起作用了。所有的行都是白色的,但扩展的子行是有颜色的。我按照material.angular.io的示例进行操作。
我看到其他关于行样式的示例,但它们使用tr标签。在示例和我的代码中,我为每一列使用了
ng-container
、th和td标签。任何帮助都将不胜感激。
我已将项目添加到stackblitz。
https://stackblitz.com/edit/angular-4bcxtv
这是我使用的HTML代码
<table mat-table
[dataSource]="dataSource" multiTemplateDataRows
class="mat-elevation-z8">
<ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
<th mat-header-cell *matHeaderCellDef> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>
<!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
<div class="example-element-detail"
[@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
<div class="example-element-diagram">
<div class="example-element-position"> {{element.position}} </div>
<div class="example-element-symbol"> {{element.symbol}} </div>
<div class="example-element-name"> {{element.name}} </div>
<div class="example-element-weight"> {{element.weight}} </div>
</div>
<div class="example-element-description">
{{element.description}}
<span class="example-element-description-attribution"> -- Wikipedia </span>
</div>
</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let element; columns: columnsToDisplay;"
class="example-element-row"
[class.example-expanded-row]="expandedElement === element"
(click)="expandedElement = expandedElement === element ? null : element">
</tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>