Angular 6 Material Data Table with Fixed Header and Paginator

Angular 6 Material Data Table with Fixed Header and Paginator

To create a Material Data Table in Angular 6 with a fixed header and paginator, follow these steps:

1. Install Angular Material

If you haven't installed Angular Material yet, you can do so by running:

ng add @angular/material 

2. Create the Component

Generate a new component for the data table:

ng generate component data-table 

3. Set Up the HTML Template

In your data-table.component.html, you can set up the Material Data Table with a fixed header and paginator.

data-table.component.html:

<div class="table-container"> <table mat-table [dataSource]="dataSource" class="mat-elevation-z8"> <!-- Define your columns here --> <ng-container matColumnDef="id"> <th mat-header-cell *matHeaderCellDef> ID </th> <td mat-cell *matCellDef="let element"> {{element.id}} </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="age"> <th mat-header-cell *matHeaderCellDef> Age </th> <td mat-cell *matCellDef="let element"> {{element.age}} </td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table> <mat-paginator [length]="data.length" [pageSize]="5" [pageSizeOptions]="[5, 10, 25]"></mat-paginator> </div> 

4. Style the Component

In your data-table.component.css, you can add styles to make the header fixed:

data-table.component.css:

.table-container { max-height: 400px; /* Set a height for the table */ overflow: auto; position: relative; } table { width: 100%; } .mat-header-cell { position: sticky; top: 0; background: white; /* Background for the fixed header */ z-index: 10; /* Ensure header is above content */ } 

5. Set Up the Component Logic

In your data-table.component.ts, set up the data source and pagination:

data-table.component.ts:

import { Component, OnInit, ViewChild } from '@angular/core'; import { MatPaginator } from '@angular/material/paginator'; import { MatTableDataSource } from '@angular/material/table'; interface DataElement { id: number; name: string; age: number; } @Component({ selector: 'app-data-table', templateUrl: './data-table.component.html', styleUrls: ['./data-table.component.css'] }) export class DataTableComponent implements OnInit { displayedColumns: string[] = ['id', 'name', 'age']; dataSource = new MatTableDataSource<DataElement>(DATA); @ViewChild(MatPaginator) paginator: MatPaginator; ngOnInit() { this.dataSource.paginator = this.paginator; } } const DATA: DataElement[] = [ {id: 1, name: 'John Doe', age: 25}, {id: 2, name: 'Jane Smith', age: 30}, {id: 3, name: 'Bob Johnson', age: 40}, // Add more data as needed ]; 

6. Import Necessary Modules

Make sure to import the necessary Angular Material modules in your main module:

app.module.ts:

import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MatTableModule } from '@angular/material/table'; import { MatPaginatorModule } from '@angular/material/paginator'; import { AppComponent } from './app.component'; import { DataTableComponent } from './data-table/data-table.component'; @NgModule({ declarations: [ AppComponent, DataTableComponent ], imports: [ BrowserModule, BrowserAnimationsModule, MatTableModule, MatPaginatorModule ], bootstrap: [AppComponent] }) export class AppModule { } 

7. Use the Component in Your Application

Include the DataTableComponent in your main application template:

app.component.html:

<app-data-table></app-data-table> 

Conclusion

With this setup, you now have a Material Data Table in Angular 6 that features a fixed header and pagination. Adjust the styles and data as needed to fit your application's requirements.

Examples

  1. Angular 6 Material Table with fixed header example Description: Implement an Angular 6 Material Data Table with a fixed header that remains visible while scrolling through a large dataset.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Columns definition --> <ng-container matColumnDef="name"> <th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th> <td mat-cell *matCellDef="let element"> {{element.name}} </td> </ng-container> <!-- Additional columns --> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table> 
  2. Angular 6 Material Table with paginator example Description: Integrate the Angular 6 Material Data Table with a paginator to handle pagination of data.

    <mat-table [dataSource]="dataSource" matSort> <!-- Columns definition --> <!-- Rows definition --> <!-- Paginator --> <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator> </mat-table> 
  3. Angular 6 Material Table with sticky header and paginator Description: Create an Angular 6 Material Data Table with a sticky header that remains fixed at the top and includes a paginator for pagination.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Columns and rows --> <!-- Sticky header --> <thead> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </thead> <!-- Paginator --> <mat-paginator [pageSizeOptions]="[5, 10, 25]" showFirstLastButtons></mat-paginator> </table> 
  4. Angular 6 Material Table with custom header and paginator Description: Customize the header of an Angular 6 Material Data Table and add a paginator for data pagination.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Custom header --> <ng-container matColumnDef="name"> <th mat-header-cell *matHeaderCellDef> Custom Header </th> <td mat-cell *matCellDef="let element"> {{element.name}} </td> </ng-container> <!-- Additional columns --> <!-- Paginator --> <mat-paginator [pageSizeOptions]="[5, 10, 25]" showFirstLastButtons></mat-paginator> </table> 
  5. Angular 6 Material Table with fixed header and sorting Description: Implement a fixed header for an Angular 6 Material Data Table with sorting functionality.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Columns and rows --> <!-- Fixed header --> <thead> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </thead> <!-- Sorting --> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </table> 
  6. Angular 6 Material Table with fixed header and row selection Description: Create an Angular 6 Material Data Table with a fixed header and enable row selection.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Columns and rows --> <!-- Fixed header --> <thead> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </thead> <!-- Row selection --> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </table> 
  7. Angular 6 Material Table with fixed header and filtering Description: Implement a fixed header for an Angular 6 Material Data Table and add filtering capabilities.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Columns and rows --> <!-- Fixed header --> <thead> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </thead> <!-- Filtering --> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </table> 
  8. Angular 6 Material Table with custom paginator styling Description: Customize the style of the paginator in an Angular 6 Material Data Table.

    <mat-paginator [pageSizeOptions]="[5, 10, 25]" showFirstLastButtons></mat-paginator> 
  9. Angular 6 Material Table with fixed header and row grouping Description: Group rows in an Angular 6 Material Data Table and add a fixed header.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Columns and rows --> <!-- Fixed header --> <thead> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </thead> <!-- Row grouping --> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </table> 
  10. Angular 6 Material Table with fixed header and cell editing Description: Enable cell editing in an Angular 6 Material Data Table with a fixed header.

    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8"> <!-- Columns and rows --> <!-- Fixed header --> <thead> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </thead> <!-- Cell editing --> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> </table> 

More Tags

background-subtraction crt database-trigger visual-studio-addins powershell-remoting controls hardware bssid delete-row propertynotfoundexception

More Programming Questions

More Mixtures and solutions Calculators

More Bio laboratory Calculators

More Chemistry Calculators

More Everyday Utility Calculators