AngularMatTable And Angular Pagination

Introduction

 
This table builds on the foundation of the cdk data-table and uses a similar interface for its data input and template. Pagination is a component that only displays page numbers. It will not manipulate your data collection. You will have to split your data collection into pages yourself.
 
Step 1
 
Let's open the command prompt. Create an Angular project by using the following command,
 
AngularMatTable And Angular Pagination
 
Step 2
 
Let's open the your created project in the Visual Studio Code and install the following command,
 
AngularMatTable And Angular Pagination
Step 3
 
Add app.module.ts page with the below code to import the references for Angular material.
  1. import { MatTableModule,MatPaginatorModule } from '@angular/material';  
  2.    
  3. imports: [  
  4.     MatTableModule,  
  5.     MatPaginatorModule,  
  6.   ],   
AngularMatTable And Angular Pagination
 
Step 4
 
After importing the module into your app module, you can now use the Material table app.component.ts import the references for MatPaginator and MatTableDataSource in your application.
  1. import { Component,OnInit,ViewChild} from '@angular/core';  
  2. import { MatPaginator, MatTableDataSource } from '@angular/material';  
AngularMatTable And Angular Pagination
 
OnInit - Called once the component Initialized
  1. dataSource = new MatTableDataSource();  
  2.   @ViewChild(MatPaginator,{statictrue}) paginator: MatPaginator;  
@ViewChild - Decorator is used for referencing elements from the components view DOM.
 
Step 5
 
displayedColumns - Let's add an array of strings to the displayedColumns and pass the array value to the matColumnDef directive.
 
AngularMatTable And Angular Pagination
 
Step 6
 
Let's create an array list of shows the details, shown below.
  1. carDetails=[  
  2.    {carname:'Hummer',model:'H3',milleage:'196321',color:'Pink'},  
  3.    {carname:'Chevrolet',model:'1500 Silverado',milleage:'195310',color:'Blue'},  
  4.    {carname:'Infiniti',model:'M',milleage:'194846',color:'Puce'},  
  5.    {carname:'Kia',model:'Amanti',milleage:'189651',color:'Indigo'},  
  6.    {carname:'Audi',model:'S5',milleage:'644541',color:'white'},  
  7.    {carname:'GMC',model:'EnvoyXUV',milleage:'187960',color:'Turquoise'},  
  8.    {carname:'Honda ',model:'H3',milleage:'45435534',color:'Pink'},  
  9.    {carname:'Maruti ',model:'ado',milleage:'195310',color:'Blue'},  
  10.    {carname:'Honda ',model:'H',milleage:'4354356',color:'Puce'},  
  11.    {carname:'TataTigor ',model:'S6',milleage:'541554',color:'Pink'},  
  12.    {carname:'Mahindra',model:'Silver',milleage:'195310',color:'Black'},  
  13.    {carname:'RenaultKWID',model:'MH',milleage:'194846',color:'Fiery Red'},  
  14.    {carname:'MahindraKwe ',model:'M5',milleage:'196321',color:'Silver White'},  
  15.    {carname:'Chevrolet',model:'Silverado',milleage:'195310',color:'Blue'},  
  16.    {carname:'Infiniti',model:'M',milleage:'194846',color:'Puce'},  
  17.  ]  
Step 7
 
In app.component.html, add the table list code.
  1.   <table class="table table-bordered" mat-table [dataSource]="dataSource">  
  2.   <ng-container matColumnDef="S.No">  
  3.     <th class="headercolor" mat-header-cell *matHeaderCellDef>S.No</th>  
  4.     <td mat-cell *matCellDef="let data let i = index "> {{paginator.pageIndex * paginator.pageSize+i+1}}  
  5.     </td>  
  6.   </ng-container>  
  7.   <ng-container matColumnDef="CarName">  
  8.     <th class="headercolor" mat-header-cell *matHeaderCellDef> CarName </th>  
  9.     <td mat-cell *matCellDef="let data  "> {{data.carname}}</td>  
  10.   </ng-container>  
  11.   <ng-container matColumnDef="Model">  
  12.     <th class="headercolor" mat-header-cell *matHeaderCellDef> Model </th>  
  13.     <td mat-cell *matCellDef="let data"> {{data.model}} </td>  
  14.   </ng-container>  
  15.   <ng-container matColumnDef="Mileage">  
  16.     <th class="headercolor" mat-header-cell *matHeaderCellDef> Mileage </th>  
  17.     <td mat-cell *matCellDef="let data"> {{data.milleage}} </td>  
  18.   </ng-container>  
  19.   <ng-container matColumnDef="Color">  
  20.     <th class="headercolor" mat-header-cell *matHeaderCellDef> Color </th>  
  21.     <td mat-cell *matCellDef="let data"> {{data.color}} </td>  
  22.   </ng-container>  
  23.   <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>  
  24.   <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>  
  25. </table>  
ng-container - We can’t use more than one structural directive in a single element. In order to avoid having to create that extra div, we can instead use ng-container directive.
 
*matHeaderRowDef - The directive is used for the a configuration object , table, and  the list of columns.
 
 *matRowDef -  This directive is used for the configuration for row cells.
 
 *matColumnDef -For each table column the <ng-container> element is used and the matColumnDef directive is applied. String with the name of that column is assigned to this directive.
 
[datasource] - The data source for the table can get the data display using the datasource property. 
 
Step 8
 
Let's add the mat-paginator in app.component.html file.
 
Use the follwing code,
  1. <mat-paginator class="pagnacolor" [pageSizeOptions]="[10]" showFirstLastButtons></mat-paginator>  
[pageSizeOptions] -The set of page size options to display with the details.
 
showFirstLastButtons- Whether to show the first or last buttons UI to the details.
 
Step 9
 
Simply need to get data from  what you stored on array list and push it to your dataSource array and add the paginator in the app.component.ts file and add the following code,
  1. getdetials()  
  2.   {  
  3.     this.dataSource = new MatTableDataSource();  
  4.     this.dataSource.data =this.carDetails;  
  5.     this.dataSource.paginator = this.paginator;  
  6.   }  
The ngOnInit method of a component is called the getdetilas method
 
Follow the  below screen screenshot,
 
AngularMatTable And Angular Pagination
 
Step 10
 
We will use the following command to start the webserver.
 
ng serve --open
 
AngularMatTable And Angular Pagination
The above image can display the details for the first page.
 

Summary

 
In this article, you have seen how you can add Material Design Table and pagination in your Angular application.