簡體   English   中英

帶有父子行的 Angular Material mat-table 的替代顏色

[英]Alternate color with Angular Material mat-table with parent child rows

我有一個 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>

dang it - 這花了我太長時間才發現,更糟的是..我不知道它為什么有效..

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

我希望我能幫助你:)

當使用mat-tablemultiTemplateDataRows ,該行通過索引dataIndex

<tr mat-row *matRowDef="let row; let i = dataIndex; columns: ['expandedDetail']" class="example-detail-row" [ngClass]="{'alt-row': i % 2}"></tr>

https://github.com/angular/components/issues/12793

您可以將下面的 css 用於表格行和 multiTemplateDataRows。 這將剝離一組表格行 + multiTemplateDataRows。

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

您可以在行上設置自己的類,然后將其作為目標,以免在 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>

我想我會跟進這個問題,因為我最近遇到了這個問題,並發現了一些有用的信息,這些信息導致了以下優雅的方法:在您的表格 html 中,我們可以利用內置的“奇數”或“偶數”屬性,具體取決於哪種顏色你要第一個。

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

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

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

我希望你可能需要這個。

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

我對 mat-option css 有問題,它必須有交替的背景。 用這個解決了它:

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

注意第二個 nth-child 前面有 ":not"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM