Search code examples
cssangular-materialmat-table

Angular 10: mat-tabel expandable rows


I am using Angular Mat-Table and have created a table with expandable rows just like the one below in Angular 10 Material exampel:

https://stackblitz.com/angular/emdqrdjmbkl?file=src%2Fapp%2Ftable-expandable-rows-example.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>

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

But then I change <table> to <mat-table>. So I did get this code below:

<mat-table
       [dataSource]="dataSource" multiTemplateDataRows
       class="mat-elevation-z8">
  <ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
    <mat-header-cell  *matHeaderCellDef> {{column}} </mat-header-cell>
    <mat-cell  *matCellDef="let element"> {{element[column]}} </mat-cell>
  </ng-container>

  <!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
  <ng-container matColumnDef="expandedDetail">
    <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>
    </mat-cell>
  </ng-container>

  <mat-header-row  *matHeaderRowDef="columnsToDisplay"></mat-header-row >
  <mat-row  mat-row *matRowDef="let element; columns: columnsToDisplay;"
      class="example-element-row"
      [class.example-expanded-row]="expandedElement === element"
      (click)="expandedElement = expandedElement === element ? null : element">
  </mat-row >
  <mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></mat-row >
</mat-table>

and css:

.mat-cell.example-detail-row {
  height: 0
}
.example-element-detail {
  overflow: hidden;
  display: flex;

}

In the exampel works fine. But here I get blank ( white ) rows.

enter image description here

Do anyone know what did I miss, or where is the problem?


Solution

  • The problem with this is, because <mat-row> take min-height:24px by default.

    So, I added css, and now works fine

    mat-row.example-detail-row {
      min-height: 0px !important;
    }