Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Pagination
The ngx-bootstrap pagination component provides your site or component with pagination links or a pager component.
selector
Inputs
Outputs
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); } }
Run the following command to start the angular server.
ng serve