In this post, we’ll discuss how to add pagination on tabular or datatable in Angular application without using a library or framework. The ngx-pagination
package module provides a component directive to quickly add pagination with a table using custom template and design.
The ngx-pagination
provides highly customized pagination solution it is fully responsive and can adjust its position according to screen size. It is packed with good looking themes and we’ll also get to know how we can customize using own styling.
We’ll create a new Angular project in latest version 9.x.x but this package is compatible with all previous major version like Angular 5,6,7 & 8.
Let’s begin with Implementation
# Update Angular CLI
Make sure to update your Angular CLI tool by running below npm command in your terminal window. You can go ahead with your current version as well
$Â npm install -g @angular/cli</pre> <h3># Create an Angular project</h3> Now, we'll create a new Angular project using the Angular CLI by running below ng command <pre class="wp-block-prismatic-blocks"><code class="language-javascript">$ ng new ngx-pagination-tutorial ? Would you like to add Angular routing? Yes ? Which stylesheet format would you like to use? CSS</pre> move to project directory <pre class="wp-block-prismatic-blocks"><code class="language-javascript">$ cd ngx-pagination-tutorial</pre> if you have Visual Studio Code install, hit below command to open the project <pre class="wp-block-prismatic-blocks"><code class="language-javascript">$ code .</pre> <h3># Install <code>ngx-pagination
Package After creating the Angular project, now we'll install thengx-pagination
package. Run following npm command in the terminal window$ npm install ngx-pagination --save</pre> <h3># Import Pagination Module</h3> Now, we'll import the <code>NgxPaginationModule
provided byngx-pagination
in the Application. In our project we'll open the app.module.ts file then import this module as shown below:// app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { NgxPaginationModule } from 'ngx-pagination'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, NgxPaginationModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
# Basic Table Styling
In this tutorial, we'll create a simple Table to add the pagination. To make Table look good, include the bootstrap.css in the index.html file's
head
section<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"></pre>
: This filter is having data related configurations with these options available.
<h3># Adding Pagination</h3>
First, to keep it simple we will implement the quickest and basic pagination with least configurations.Implement Pagination in Component. Open <strong>app.component.html</strong> and add the following code
<code><strong>paginate</strong>
<div style="text-align:center">
: This is passing the current page to the configuration.
<h4>
Basic Pagination
</h4><table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Item</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of collection.data | paginate: config">
<th scope="row">{{item.id}}</th>
<td>{{item.value}}</td>
</tr>
</tbody>
</table><pagination-controls (pageChange)="pageChanged($event)"></pagination-controls>
</div></pre>
In the <strong>app.component.ts</strong> file replace below code
<div>
<div><code><strong>pageChanged</strong>
Leave a Reply