【发布时间】:2019-10-14 02:35:32
【问题描述】:
我有一个 Angular Material mat-table,我使用 CSS 样式来替代行颜色。这是 CSS 样式:
.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>
【问题讨论】:
-
你能提供一个stackblitz来显示这个问题吗?
-
stackblitz 链接是否为您运行?有很多错误
-
对不起,我发布了代码,但我是 stackblitz 的新手。该代码适用于我的 Visual Studio 代码......但我无法让它在线工作
标签: css angular angular-material mat-table