Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Pagination

Ngx Bootstrap Pagination

Ngx Bootstrap Pagination

The ngx-bootstrap pagination component provides your site or component with pagination links or a pager 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, custom template for first link
  • customLastTemplate − TemplateRef, custom template for last link
  • customNextTemplate − TemplateRef, custom template for next link
  • customPageTemplate − TemplateRef, custom template for page link
  • customPreviousTemplate − TemplateRef, 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
  •  
  • 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

We have to update the app.module.ts used in the ngx-bootstrap Modals chapter to use PaginationModule and PaginationConfig as we are going to use a pagination.

To use the PaginationModule and the PaginationConfig, update app.module.ts.

To use the PaginationModule and the PaginationConfig, update app.module.ts.

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