Angular mat-table – sticky header doesn't works

Issue

I am using Angular Material and want to have fixed headers.
I saw many solutions already, but none works for me.

Normally it should work by adding “sticky: true” to the “mat-header-row *matHeaderRowDef=”, but not for me…
I checked my Angular Material & Angular CDK version, they are up-to-date.

html:

<mat-table #table [dataSource]="dataSource">

  <ng-container matColumnDef="fromTo">
    <mat-header-cell *matHeaderCellDef> Absender/Empfänger </mat-header-cell>
    <mat-cell *matCellDef="let entry">{{entry.customer.shownName}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="forwarder">
    <mat-header-cell *matHeaderCellDef> Spediteur </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.forwarder.shownName}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="binType">
    <mat-header-cell *matHeaderCellDef> BehälterTyp </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.bin.name}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="inboundQty">
    <mat-header-cell *matHeaderCellDef> Eingang </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.inboundQty}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="outboundQty">
    <mat-header-cell *matHeaderCellDef> Ausgang </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.outboundQty}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="comment">
    <mat-header-cell *matHeaderCellDef> Kommentar </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.comment}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="user">
    <mat-header-cell *matHeaderCellDef> Benutzer </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.user.email}} </mat-cell>
  </ng-container>

  <mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

package.json:

  "@angular/cdk": "^7.0.0",
    "@angular/material": "^7.0.0",

any idea why “sticky:true” doesn’t works for me?

Thanks in advance

Solution

Try this…
Add <div class="example-container><div>

<div class="example-container >
  <mat-table #table [dataSource]="dataSource">

  <ng-container matColumnDef="fromTo">
    <mat-header-cell *matHeaderCellDef> Absender/Empfänger </mat-header-cell>
    <mat-cell *matCellDef="let entry">{{entry.customer.shownName}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="forwarder">
    <mat-header-cell *matHeaderCellDef> Spediteur </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.forwarder.shownName}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="binType">
    <mat-header-cell *matHeaderCellDef> BehälterTyp </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.bin.name}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="inboundQty">
    <mat-header-cell *matHeaderCellDef> Eingang </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.inboundQty}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="outboundQty">
    <mat-header-cell *matHeaderCellDef> Ausgang </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.outboundQty}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="comment">
    <mat-header-cell *matHeaderCellDef> Kommentar </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.comment}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="user">
    <mat-header-cell *matHeaderCellDef> Benutzer </mat-header-cell>
    <mat-cell *matCellDef="let entry"> {{entry.user.email}} </mat-cell>
  </ng-container>

  <mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>
</div>

Add this CSS

.example-container {
  height: 400px;
  overflow: auto;
}

Answered By – Sooriya Dasanayake

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.