In this article, you’ll see a simple example of how to implement client-side pagination in Angular 8.
Example built with Angular 8.0.0
The example contains a hard coded array of 150 objects split into 15 pages to demonstrate how the pagination component works.
Pagination is implemented with the `` component that comes with the jw-angular-pagination
package available on npm.
Install the Angular 8 pagination component with the command npm install jw-angular-pagination
.
Import the JwPaginationComponent
into your Angular app.module.ts
and add it to the declarations
array to make it available to other components within the Angular module.
This is the app module (app.module.ts
) from the example, the pagination component is imported on line 3
and added to the declarations on line 13
.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { JwPaginationComponent } from 'jw-angular-pagination';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule
],
declarations: [
AppComponent,
JwPaginationComponent
],
bootstrap: [AppComponent]
})
export class AppModule { }
There are 2 required properties for using the Angular 8 pagination component:
changePage
eventThere are also a few optional properties:
This is the app component (app.component.ts
) from the example, it creates a hardcoded array of items to be paged in the ngOnInit()
method, and updates the current page of items in the onChangePage()
callback method.
import { Component, OnInit } from '@angular/core';
@Component({ selector: 'app', templateUrl: 'app.component.html' })
export class AppComponent implements OnInit {
items = [];
pageOfItems: Array;
constructor() { }
ngOnInit() {
// an example array of 150 items to be paged
this.items = Array(150).fill(0).map((x, i) => ({ id: (i + 1), name: `Item ${i + 1}`}));
}
onChangePage(pageOfItems: Array) {
// update current page of items
this.pageOfItems = pageOfItems;
}
}
This is the app component template (app.component.html
) from the example, it renders the current page of items using the *ngFor
Angular directive on line 5
, and includes the pagination component (``) on line 8
.
The pagination component is bound to items
property of the app component using the Angular model binding attribute [items]="items"
, and is bound to the onChangePage()
method of the app component using the Angular event binding attribute (changePage)="onChangePage($event)"
.
### Angular 8 Pagination Example
{{item.name}}
The JW Angular pagination component is unstyled by default, you can use the below CSS selectors to add your own custom styles.
You can also plug in Bootstrap (3.x or 4.x) which the component works well with, that’s what I used in the example.
ul
element)To hide any of the buttons you can simply set them to display: none;
using the css selectors described above.
If you want to make other customisations such as changing the HTML template of the component, I’d recommend just copying the pagination component code into your own custom Angular component, it’s only 60 lines and will give complete flexibility to make any changes you like.
To use this approach you need to install the jw-paginate
package from npm with the command npm install jw-paginate
. The jw-paginate
package contains the pagination logic used to paginate any array or list of items. For more info about the pagination logic see this post.
This is the complete pagination component code, it’s also available here on GitHub.
import { Component, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import paginate = require('jw-paginate');
@Component({
moduleId: module.id,
selector: 'jw-pagination',
template: `
First
Previous
{{page}}
Next
Last
`
})
export class JwPaginationComponent implements OnInit, OnChanges {
@Input() items: Array;
@Output() changePage = new EventEmitter(true);
@Input() initialPage = 1;
@Input() pageSize = 10;
@Input() maxPages = 10;
pager: any = {};
ngOnInit() {
// set page if items array isn't empty
if (this.items && this.items.length) {
this.setPage(this.initialPage);
}
}
ngOnChanges(changes: SimpleChanges) {
// reset page if items array has changed
if (changes.items.currentValue !== changes.items.previousValue) {
this.setPage(this.initialPage);
}
}
private setPage(page: number) {
// get new pager object for specified page
this.pager = paginate(this.items.length, page, this.pageSize, this.maxPages);
// get new page of items from items array
var pageOfItems = this.items.slice(this.pager.startIndex, this.pager.endIndex + 1);
// call change page function in parent component
this.changePage.emit(pageOfItems);
}
}
#angular #typescript