Ngx-Bootstrap - Pagination



ngx-bootstrap pagination component provides pagination links or a pager component to your site or component.

PaginationComponent

selector

  • pagination

Inputs

  • align − boolean, if true aligns each link to the sides of pager

  • boundaryLinks − boolean, if false first and last buttons will be hidden

  • customFirstTemplate − TemplateRef<PaginationLinkContext>, custom template for first link

  • customLastTemplate − TemplateRef<PaginationLinkContext>, custom template for last link

  • customNextTemplate − TemplateRef<PaginationLinkContext>, custom template for next link

  • customPageTemplate − TemplateRef<PaginationLinkContext>, custom template for page link

  • customPreviousTemplate − TemplateRef<PaginationLinkContext>, custom template for previous link

  • directionLinks − boolean, if false previous and next buttons will be hidden

  • disabled − boolean, if true pagination component will be disabled

  • firstText − boolean, first button text

  • itemsPerPage − number, maximum number of items per page. If value less than 1 will display all items on one page

  • lastText − string, last button text

  • maxSize − number, limit number for page links in pager

  • nextText − string, next button text

  • pageBtnClass − string, add class to <li>

  • previousText − string, previous button text

  • rotate − boolean, if true current page will in the middle of pages list

  • totalItems − number, total number of items in all pages

Outputs

  • numPages − fired when total pages count changes, $event:number equals to total pages count.

  • pageChanged − fired when page was changed, $event:{page, itemsPerPage} equals to object with current page index and number of items per page.

Example

As we're going to use a pagination, We've to update app.module.ts used in ngx-bootstrap Modals chapter to use PaginationModule and PaginationConfig.

Update app.module.ts to use the PaginationModule and PaginationConfig.

app.module.ts

import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { AppComponent } from './app.component'; import { TestComponent } from './test/test.component'; import { AccordionModule } from 'ngx-bootstrap/accordion'; import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert'; import { ButtonsModule } from 'ngx-bootstrap/buttons'; import { FormsModule } from '@angular/forms'; import { CarouselModule } from 'ngx-bootstrap/carousel'; import { CollapseModule } from 'ngx-bootstrap/collapse'; import { BsDatepickerModule, BsDatepickerConfig } from 'ngx-bootstrap/datepicker'; import { BsDropdownModule,BsDropdownConfig } from 'ngx-bootstrap/dropdown'; import { PaginationModule,PaginationConfig } from 'ngx-bootstrap/pagination'; @NgModule({ declarations: [ AppComponent, TestComponent ], imports: [ BrowserAnimationsModule, BrowserModule, AccordionModule, AlertModule, ButtonsModule, FormsModule, CarouselModule, CollapseModule, BsDatepickerModule.forRoot(), BsDropdownModule, ModalModule, PaginationModule ], providers: [AlertConfig, BsDatepickerConfig, BsDropdownConfig, BsModalService, PaginationConfig], bootstrap: [AppComponent] }) export class AppModule { }

Update test.component.html to use the modal.

test.component.html

<div class="row"> <div class="col-xs-12 col-12"> <div class="content-wrapper"> <p class="content-item" *ngFor="let content of returnedArray">{{content}}</p> </div> <pagination [boundaryLinks]="showBoundaryLinks" [directionLinks]="showDirectionLinks" [totalItems]="contentArray.length" [itemsPerPage]="5" (pageChanged)="pageChanged($event)"></pagination> </div> </div> <div> <div class="checkbox"> <label><input type="checkbox" [(ngModel)]="showBoundaryLinks">Show Boundary Links</label> <br/> <label><input type="checkbox" [(ngModel)]="showDirectionLinks">Show Direction Links</label> </div> </div>

Update test.component.ts for corresponding variables and methods.

test.component.ts

import { Component, OnInit } from '@angular/core'; import { BsModalService } from 'ngx-bootstrap/modal'; import { PageChangedEvent } from 'ngx-bootstrap/pagination'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] }) export class TestComponent implements OnInit { contentArray: string[] = new Array(50).fill(''); returnedArray: string[]; showBoundaryLinks: boolean = true; showDirectionLinks: boolean = true; constructor() {} pageChanged(event: PageChangedEvent): void { const startItem = (event.page - 1) * event.itemsPerPage; const endItem = event.page * event.itemsPerPage; this.returnedArray = this.contentArray.slice(startItem, endItem); } ngOnInit(): void { this.contentArray = this.contentArray.map((v: string, i: number) => { return 'Line '+ (i + 1); }); this.returnedArray = this.contentArray.slice(0, 5); } }

Build and Serve

Run the following command to start the angular server.

ng serve

Once server is up and running. Open http://localhost:4200. Click on Open modal button and verify the following output.

Pagination
Advertisements