46

How to make the Header of Data Table component fixed to the top, and the Paginator fixed to the bottom?

This is my HTML:

<div class="example-container mat-elevation-z8">

    <table mat-table #itemsTable [dataSource]="dataSource" class="items-list">

        <ng-container matColumnDef="position">
            <th mat-header-cell *matHeaderCellDef> No. </th>
            <td mat-cell *matCellDef="let element"> {{element.position}} </td>
        </ng-container>

        <ng-container matColumnDef="name">
            <th mat-header-cell *matHeaderCellDef> Name </th>
            <td mat-cell *matCellDef="let element"> {{element.name}} </td>
        </ng-container>

        <ng-container matColumnDef="weight">
            <th mat-header-cell *matHeaderCellDef> Weight </th>
            <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
        </ng-container>

        <ng-container matColumnDef="symbol">
            <th mat-header-cell *matHeaderCellDef> Symbol </th>
            <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
        </ng-container>

        <tr mat-header-row *matHeaderRowDef="this.componentsDataService.displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: this.componentsDataService.displayedColumns;" (click)="onRowClicked(row)"></tr>
    </table>

    <mat-paginator [pageSizeOptions]="[50, 100, 250]" showFirstLastButtons></mat-paginator>

</div>

I tried adding sticky to the <tr mat-header-row> from the docs, but it doesn't work.

My imports in .ts file:

 import { Component, OnInit, OnChanges, Input, ViewChild } from '@angular/core';
 import { TabsDataService } from 'src/app/services/tabs-data.service';
 import { ComponentsDataService } from 'src/app/services/components-data.service';
 import { MatPaginator, MatTableDataSource, MatTable, MatTableModule } from '@angular/material';
DJ Burb
  • 2,346
  • 2
  • 29
  • 38
Chris K
  • 820
  • 3
  • 10
  • 18

7 Answers7

127

I found in the examples on the material site that the header can be fixed by adding sticky to the matHeaderRowDef:

<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true">

For the paginator i added a class to mat-paginator:

<mat-paginator ...
                class="mat-paginator-sticky"> 

with a class based on the answer in the link @frederik provided in the comments

.mat-paginator-sticky {
  bottom: 0px;
  position: sticky;
  z-index: 10;
}
Wendie
  • 1,386
  • 2
  • 10
  • 5
22

As per Material 8.1

  1. For the sticky header we need to give a sticky values as shown in many answers here

  2. For the sticky paginator or bottom fixed paginator.

We can wrap the table inside a div with max-height or height CSS Property and put the paninator outside that div.

Html Code sample.

 <div class="mat-elevation-z2">
    <div class="table-container">

      <!--- List of column definitions here --->
      <table mat-table [dataSource]="dataSource" matSort>
        <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
      </table>

    </div>
    <mat-paginator [pageSize]="25" [pageSizeOptions]="[10, 15, 25, 100]"></mat-paginator>
 </div>

CSS

  table {
    width: 100%;
  }

  .table-container {
    max-height: calc(100vh - 155px); // or height: calc(100vh - 155px); depending on your need  change
    overflow: auto;
  }
WasiF
  • 26,101
  • 16
  • 120
  • 128
Morlo Mbakop
  • 3,518
  • 20
  • 21
  • This is one of the best solutions. I created a example by also set first column to sticky: https://stackblitz.com/edit/angular-p6zdti – Jorgen Apr 08 '20 at 04:55
  • This is good if you want your whole table to be essentially a fixed height, which in most cases, I think acts most naturally. You might want to scroll down a list by scrolling your whole screen, but I think users expect tables (esp. with paginators) to have a fixed size. – Adam Dunkerley Jun 03 '21 at 17:09
14

Try to update angular material in your project because the sticky attribute was added in 6.2.3.

kavind
  • 261
  • 2
  • 13
5

The below css worked for me:

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
.mat-header-row{
  position: sticky;
  top: 0;
  z-index: 100;
  background: white;
  }
Nick
  • 138,499
  • 22
  • 57
  • 95
  • 1
    I don't know what is wrong, but when I try to pin header by means of css .mat-header-row{ position: sticky; it doesn't work. Only specifying explicitly in html sticky: true helps – MaterialGirl Jan 28 '19 at 08:53
  • 1
    @M.Laida It shouldn't, as `sticky` position is not in IE11. – Prajwal Apr 15 '19 at 09:49
3

I too had this problem. I created a div of fixed size and then kept the table inside div. This should solve your issue.

<div style="height: 300px; overflow: auto">
    <table mat-table [dataSource]=dataSource>
        <!--Your table entries here -->
        . 
        .
        .
        .
        <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
</div>
Eranki
  • 750
  • 1
  • 11
  • 30
2

You can use a normal sticky mat-footer-row with colspan and put the paginator inside:

<table mat-table>
   ...

    <ng-container matColumnDef="paginator">
        <td mat-footer-cell *matFooterCellDef [colSpan]="displayedColumns.length">
            <mat-paginator ...></mat-paginator>
        </td>
    </ng-container>

    <tr mat-footer-row *matFooterRowDef="['paginator']; sticky: true"></tr>
</table>

And use some style like this, to make it more appealing to eye:

.mat-row:last-child td {
  border-bottom: none;
}

.mat-footer-row:first-child td {
  border-top: 1px solid rgba(0, 0, 0, 0.12);
}

.mat-footer-cell .mat-paginator {
  margin-left: -24px;
  margin-right: -24px;
}
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
  • This is too hacky, We can wrap the table inside a div with max-height or height CSS Property, the issue will be solved. – Morlo Mbakop Aug 03 '19 at 05:21
  • 3
    This was the only solution that I could get working with a variable-height window, without having to do a vh calculation. Might be hacky but at least it does what I want! – Jonathan Sep 01 '19 at 01:50
0

Note that my mistake was to use

<mat-header-cell ...
<mat-cell ...

instead of

<th mat-header-cell ...
<td mat-cell 
Pipo
  • 4,653
  • 38
  • 47