- Simple Datatable
- Data table With Pagination
- Data table With Sorting
- ng g c withfiltering
- <mat-card>
- <div class="alert alert-info">
- <strong>Angular Material DataTable With Filtering</strong>
- </div>
- <div class="example-container mat-elevation-z8">
- <div class="">
- <mat-form-field style="width: 98%;">
- <input matInput (keyup)="Filter($event.target.value)" placeholder="Filter">
- </mat-form-field>
- </div>
- <mat-table #Table [dataSource]="MyDataSource">
- <!-- For ID -->
- <ng-container matColumnDef="id">
- <mat-header-cell *matHeaderCellDef> ID </mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.id}} </mat-cell>
- </ng-container>
- <!-- For Post ID -->
- <ng-container matColumnDef="postId">
- <mat-header-cell *matHeaderCellDef> Post ID </mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.postId}} </mat-cell>
- </ng-container>
- <!-- For Name -->
- <ng-container matColumnDef="name">
- <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.name}} </mat-cell>
- </ng-container>
- <!-- For Email ID -->
- <ng-container matColumnDef="email">
- <mat-header-cell *matHeaderCellDef> Email ID </mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.email}} </mat-cell>
- </ng-container>
- <!-- For Body -->
- <ng-container matColumnDef="body">
- <mat-header-cell *matHeaderCellDef> Body Text</mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.body}} </mat-cell>
- </ng-container>
- <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
- <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
- </mat-table>
- <!-- To paginate between pages with search -->
- <mat-paginator #paginator [pageSize]="10" [pageSizeOptions]="[5, 10, 20]">
- </mat-paginator>
- </div>
- </mat-card>
- <mat-form-field style="width: 98%;">
- <input matInput (keyup)="Filter($event.target.value)" placeholder="Filter">
- </mat-form-field>
- import { Component, OnInit ,ViewChild} from '@angular/core';
- import { MatTableDataSource,MatPaginator } from '@angular/material';
- import { DataServiceService } from '../service/data-service.service';
- import { MatSort } from '@angular/material';
- import { BehaviorSubject } from 'rxjs/BehaviorSubject';
- import { Observable } from 'rxjs/Observable';
- @Component({
- selector: 'app-withfiltering',
- templateUrl: './withfiltering.component.html',
- styleUrls: ['./withfiltering.component.css']
- })
- export class WithfilteringComponent implements OnInit {
- MyDataSource: any;
- displayedColumns = ['id', 'postId','name','email','body'];
- @ViewChild(MatPaginator) paginator: MatPaginator;
- constructor(public dataService: DataServiceService) { }
- ngOnInit() {
- this.RenderDataTable();
- }
- RenderDataTable() {
- this.dataService.GetAllComments()
- .subscribe(
- res => {
- this.MyDataSource = new MatTableDataSource();
- this.MyDataSource.data = res;
- this.MyDataSource.paginator = this.paginator;
- console.log(this.MyDataSource.data);
- },
- error => {
- console.log('There was an error while retrieving Comments !!!' + error);
- });
- }
- Filter(searchstring:string)
- {
- searchstring = searchstring.trim();
- searchstring = searchstring.toLowerCase();
- this.MyDataSource.filter = searchstring;
- }
- }
- filter()
- // To fill the Datatable with Comments [Dummy Data]
- public GetAllComments() {
- return this.http.get(this._baseUrl + 'comments')
- .map((res: Response) => {
- return res.json();
- })
- .catch(this.handleError);
- }
So far, we have implemented filter functionality using Angular Material Datatable, now it's time to see how it works.

- ng g c combined
Now, open the combined.component.html file and replace it with the following code snippet.
- <mat-card>
- <div class="alert alert-info">
- <strong>Combined Angular DataTable Demo With All Functionality</strong>
- </div>
- <div class="example-container mat-elevation-z8">
- <!-- For Filtering -->
- <div class="">
- <mat-form-field style="width: 98%;">
- <input matInput (keyup)="Filter($event.target.value)" placeholder="Filter">
- </mat-form-field>
- </div>
- <mat-table #Table [dataSource]="MyDataSource" matSort>
- <!-- For ID -->
- <ng-container matColumnDef="id">
- <mat-header-cell *matHeaderCellDef mat-sort-header> ID </mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.id}} </mat-cell>
- </ng-container>
- <!-- For User ID -->
- <ng-container matColumnDef="userId">
- <mat-header-cell *matHeaderCellDef mat-sort-header> User ID </mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.userId}} </mat-cell>
- </ng-container>
- <!-- For Title -->
- <ng-container matColumnDef="title">
- <mat-header-cell *matHeaderCellDef mat-sort-header> Title </mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.title}} </mat-cell>
- </ng-container>
- <!-- For Completion Status -->
- <ng-container matColumnDef="completed">
- <mat-header-cell *matHeaderCellDef mat-sort-header> Completion Status</mat-header-cell>
- <mat-cell *matCellDef="let post"> {{post.completed}} </mat-cell>
- </ng-container>
- <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
- <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
- </mat-table>
- <!-- To paginate between pages with search -->
- <mat-paginator #paginator [pageSize]="10" [pageSizeOptions]="[5, 10, 20]">
- </mat-paginator>
- </div>
- </mat-card>
This HTML file contains all of the code which is used to implement the datatable with all the functionalities.
- import { Component, OnInit ,ViewChild} from '@angular/core';
- import { MatTableDataSource,MatPaginator,MatSort } from '@angular/material';
- import { DataServiceService } from '../service/data-service.service';
- import { BehaviorSubject } from 'rxjs/BehaviorSubject';
- import { Observable } from 'rxjs/Observable';
- @Component({
- selector: 'app-combined',
- templateUrl: './combined.component.html',
- styleUrls: ['./combined.component.css']
- })
- export class CombinedComponent implements OnInit {
- MyDataSource: any;
- displayedColumns = ['id', 'userId','title','completed'];
- @ViewChild(MatPaginator) paginator: MatPaginator;
- @ViewChild(MatSort) sort: MatSort;
- constructor(public dataService: DataServiceService) { }
- ngOnInit() {
- this.RenderDataTable();
- }
- RenderDataTable() {
- this.dataService.GetAllTodos()
- .subscribe(
- res => {
- this.MyDataSource = new MatTableDataSource();
- this.MyDataSource.data = res;
- this.MyDataSource.sort = this.sort;
- this.MyDataSource.paginator = this.paginator;
- console.log(this.MyDataSource.data);
- },
- error => {
- console.log('There was an error while retrieving Todos !!!' + error);
- });
- }
- Filter(searchstring:string)
- {
- searchstring = searchstring.trim();
- searchstring = searchstring.toLowerCase();
- this.MyDataSource.filter = searchstring;
- }
- }
It contains the all necessary code used for a different operation like Pagination, Filtering, Sorting etc.
- // To fill the Datatable with Todos [Dummy Data]
- public GetAllTodos() {
- return this.http.get(this._baseUrl + 'todos')
- .map((res: Response) => {
- return res.json();
- })
- .catch(this.handleError);
- }
We are done with our example, now let's quickly run our example and see the complete and full fledged working Angular Material Datatable with different functionalities.

Laxmidhar SahooPosted Nov 13, 2018, 1:34 AM
Thanks for a good naration
EMMANUEL OKELLOPosted Oct 25, 2018, 8:01 AM
Great, thanks, exactly what I was looking for. May you please demonstrate how to add debounceTime and distinctUntilChanged to avoid rapidly hitting the backend.