[英]Alternate color with Angular Material mat-table with parent child rows
I have an Angular Material mat-table
which I used CSS styling for alternate row colors.我有一个 Angular Material
mat-table
,我将 CSS 样式用于替代行颜色。 Here is the CSS styling:这是 CSS 样式:
.mat-row:nth-child(even){
background-color: #e4f0ec;
}
.mat-row:nth-child(odd){
background-color:#ffffff;
}
This works when my mat-table does not have defined child rows.当我的 mat-table 没有定义子行时,这有效。 When I added another
ng-container
which is specified with "expandedDetail", this does not work anymore.当我添加另一个用“expandedDetail”指定的
ng-container
,这不再起作用。 All the rows is white but the expanded child rows are colored.所有行都是白色的,但展开的子行是彩色的。
I followed the example from material.angular.io我按照 material.angular.io 中的示例进行操作
I see other example for row styling but they use tr tags.我看到了行样式的其他示例,但他们使用 tr 标签。 In the example and my code, I used an
ng-container
, th and td tags for each column.在示例和我的代码中,我为每一列使用了
ng-container
、 th 和 td 标签。
Any help is appreciated.任何帮助表示赞赏。
I added the project to stackblitz我将项目添加到 stackblitz
https://stackblitz.com/edit/angular-4bcxtv https://stackblitz.com/edit/angular-4bcxtv
Here is the HTML code I used这是我使用的 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>
dang it - this took me too long to find out and the worse.. I don't know excatly why it's working.. dang it - 这花了我太长时间才发现,更糟的是..我不知道它为什么有效..
.mat-row:nth-child(2n+1){
background-color: #e4f0ec;
}
.mat-row:not(:nth-child(4n+1)){
background-color:#ffffff;
}
I hope I could help you :)我希望我能帮助你:)
When using mat-table
with multiTemplateDataRows
, the rows are indexed by dataIndex
.当使用
mat-table
与multiTemplateDataRows
,该行通过索引dataIndex
。
<tr mat-row *matRowDef="let row; let i = dataIndex; columns: ['expandedDetail']" class="example-detail-row" [ngClass]="{'alt-row': i % 2}"></tr>
See https://github.com/angular/components/issues/12793见https://github.com/angular/components/issues/12793
You can use below css for table row and multiTemplateDataRows.您可以将下面的 css 用于表格行和 multiTemplateDataRows。 This will strip group of table row + multiTemplateDataRows.
这将剥离一组表格行 + multiTemplateDataRows。
.mat-row:nth-child(4n-1), .mat-row:nth-child(4n) {
background-color: #e4f0ec;
}
You could set your own class on the row and then target it, so as to not mess around with n-depth and complex selectors that break as your html changes.您可以在行上设置自己的类,然后将其作为目标,以免在 html 更改时破坏 n-depth 和复杂的选择器。
.my-row {
background-color: #e4f0ec;
}
.my-row__alternate {
background-color:#ffffff;
}
<tr mat-row *matRowDef="let element; columns: columnsToDisplay; let i = index"
class="my-row"
[class.my-row__alternate]="i % 2">
</tr>
Thought I would follow up on this issue as I recently faced it and found pieces of useful information that lead to the following elegant approach: In your table html we can utilize the built in "odd" or "even" properties, depending on which color you want first.我想我会跟进这个问题,因为我最近遇到了这个问题,并发现了一些有用的信息,这些信息导致了以下优雅的方法:在您的表格 html 中,我们可以利用内置的“奇数”或“偶数”属性,具体取决于哪种颜色你要第一个。
<tr mat-row *matRowDef="let row; let odd = odd; columns: columnNames;" [class.striped-row]="odd"></tr>
Then in your CSS add the following:然后在你的 CSS 中添加以下内容:
.striped-row {
background-color: #ececec;
}
I hope you might need this.我希望你可能需要这个。
.mat-row:nth-child(2n+1) {
background-color: #FFFFFF;
}
.mat-row:nth-child(2n) {
background-color: #F2F5F7;
}
.mat-row:nth-child(4n) {
background-color: #FFFFFF;
}
.mat-row:nth-child(4n+1) {
background-color: #F2F5F7;
}
I had issue with mat-option css, it had to have alternating background.我对 mat-option css 有问题,它必须有交替的背景。 Solved it with this:
用这个解决了它:
.mat-option {
&:nth-child(2n+1) {
background-color: #f0efff;
}
&:not(:nth-child(2n+1)) {
background-color: #fff;
}
}
Note that second nth-child has ":not" in front注意第二个 nth-child 前面有 ":not"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.