English 中文(简体)
Ngx-Bootstrap - Pagination
  • 时间:2024-11-03

Ngx-Bootstrap - Pagination


Previous Page Next Page  

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

PaginationComponent

selector

    pagination

Inputs

    apgn − boolean, if true apgns each pnk to the sides of pager

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

    customFirstTemplate − TemplateRef<PaginationLinkContext>, custom template for first pnk

    customLastTemplate − TemplateRef<PaginationLinkContext>, custom template for last pnk

    customNextTemplate − TemplateRef<PaginationLinkContext>, custom template for next pnk

    customPageTemplate − TemplateRef<PaginationLinkContext>, custom template for page pnk

    customPreviousTemplate − TemplateRef<PaginationLinkContext>, custom template for previous pnk

    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, pmit number for page pnks in pager

    nextText − string, next button text

    pageBtnClass − string, add class to <p>

    previousText − string, previous button text

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

    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


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

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.spce(startItem, endItem);
   }
   ngOnInit(): void {
      this.contentArray = this.contentArray.map((v: string, i: number) => {
         return  Line  + (i + 1);
      });
      this.returnedArray = this.contentArray.spce(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. Cpck on Open modal button and verify the following output.

Pagination Advertisements