使用Angular Material mat-table实现父子行交替颜色

9
我可以帮忙翻译。这段内容是关于如何使用 CSS 样式来为 Angular Material 的 mat-table 添加交替行颜色的。以下是 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 来展示这个问题吗? - ConnorsFan
https://stackblitz.com/edit/angular-4bcxtv - c0micrage
这个 StackBlitz 链接能运行吗?有一些错误。 - tic
抱歉,我在 StackBlitz 上是新手,我已经发布了代码。这段代码在我的 Visual Studio Code 上可以运行……但我无法让它在线上运行。 - c0micrage
8个回答

11

真糟糕 - 我花了太长时间才发现,更糟糕的是,我不确切知道它为什么能起作用..

.mat-row:nth-child(2n+1){
  background-color: #e4f0ec;
}
.mat-row:not(:nth-child(4n+1)){
  background-color:#ffffff;
}

我希望我能帮到你 :)


5

我想跟进这个问题,因为我最近遇到了它,并找到了一些有用的信息片段,从而得出以下优雅的方法: 在您的表格HTML中,我们可以利用内置的“odd”或“even”属性,具体取决于您想要哪种颜色先显示。

<tr mat-row *matRowDef="let row; let odd = odd; columns: columnNames;" [class.striped-row]="odd"></tr>

然后在你的CSS中添加以下内容:

.striped-row {
    background-color: #ececec;
}

3
使用带有`multiTemplateDataRows`的`mat-table`时,行由`dataIndex`索引。
<tr mat-row *matRowDef="let row; let i = dataIndex; columns: ['expandedDetail']" class="example-detail-row" [ngClass]="{'alt-row': i % 2}"></tr>

请查看 {{link1}}

2
您可以使用以下CSS样式来处理表格行和多模板数据行。这将去除一组表格行和多模板数据行。
返回结果如下:

您可以使用以下CSS样式来处理表格行和多模板数据行。这将去除一组表格行和多模板数据行。

.mat-row:nth-child(4n-1), .mat-row:nth-child(4n) {
    background-color: #e4f0ec;
}

这更像是预期的结果。被接受的答案总是以相同的颜色呈现子行。上面的答案将子行涂成与父行相同的颜色。 - Jendrik

2

您可以在行上设置自己的类,然后针对它进行目标设置,以便不会使用 n 层和复杂选择器而破坏HTML更改。

.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>

0

.mat-mdc-row:nth-child(2n+1){
  background-color: rgba(0,0,0,.03);
}
.mat-mdc-row:nth-child(2n){
  background-color:#ffffff;
}

我不知道是不是因为 mat-table 的版本问题,但是我不得不使用 mat_mdc_row 类来使其正常工作。

0

我在mat-option CSS方面有问题,它需要交替的背景色。我用以下方法解决了这个问题:

.mat-option {
    &:nth-child(2n+1) {
        background-color: #f0efff;
    }
    &:not(:nth-child(2n+1)) {
        background-color: #fff;
    }
}

请注意第二个nth-child前面有":not"。

0

希望你可能需要这个。

   .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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接