Integrate pager component with listview in Angular Listview component

27 Sep 20234 minutes to read

The first and foremost step is to obtain the Pager component from Grid. Install the ej2-angular-grids package using the following command.

npm install @syncfusion/ej2-angular-grids --save

Import the Pager to the ListView sample which has been created.

import { Pager } from "@syncfusion/ej2-angular-grids";

The totalRecordsCount property of the Pager must be specified whenever using this particular component. By using pageSize property, the number of list items to be displayed is made available. The pageCount property allows the user to specify the visibility of the page numbers accordingly. Since the paging sample in the upcoming code snippet uses these three properties, the explanation provided here are minimal and to the point. For further API concerns in Pager component, click here.

With the help of the query property of ListView, the user can specify the number of records to be displayed in the current page.

The query property helps in splitting the entire datasource based on the user’s convenience. In the sample provided below, when clicking the next button in pager, it fetches the datasource based on the page size and the current page of the Pager component.

The headerTemplate and the template property of ListView is defined within ng-template. The required styles can be changed here accordingly.

public clickevent(args) {
  this.query = new Query().range((args.currentPage - 1) * this.pagesize, (args.currentPage * this.pagesize));
}

In the above code snippet, the event stores the currentPage value, and the datasource which is to be displayed in the next page is obtained.

Note: When pageSize isn’t mentioned, it defaults to 12 records per page.

import { Component} from '@angular/core';
import { Browser } from '@syncfusion/ej2-base';
import { datasource } from './datasource';
import { DataManager, Query, JsonAdaptor } from '@syncfusion/ej2-data';
import { Pager } from "@syncfusion/ej2-angular-grids";
@Component({
    selector: 'my-app',
    template: `<div class="control-section">
  <ejs-listview id='listview' [dataSource]='data' [query]='query' showHeader='true' >
    <ng-template #headerTemplate let-data="">
      <table class="w-100"> <tr><td class="w-25">Order ID</td><td class="w-45">Ship       Name</td><td class="w-25">Ship City</td></tr></table>
    </ng-template>
    <ng-template #template let-data="">
      <table class="w-100"> <tr><td class="w-25"></td><td class="w-45"></td><td class="w-25" ></td></tr></table>
    </ng-template>
  </ejs-listview>
  <ejs-pager [pageSize]= 'pagesize' [totalRecordsCount]='totalrec' [pageCount]='pageCount' (click)='clickevent($event)'>
  </ejs-pager>
</div>`
})

export class AppComponent {
public totalrec: number = datasource.length;
public pagesize: number = 10;
public pageCount: number = 2;
//Define an array of JSON data
public data: Object = new DataManager({
    json: datasource,
    adaptor: new JsonAdaptor
});
public query = new Query().range(0,this.pagesize);
  templatecheck: boolean;
public clickevent(args: any) {
  this.query = new Query().range((args.currentPage - 1) * this.pagesize, (args.currentPage * this.pagesize));
}
constructor(){
    this.templatecheck = Browser.isDevice;
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ListViewModule } from '@syncfusion/ej2-angular-lists';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
import { PagerModule } from '@syncfusion/ej2-angular-grids';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        ListViewModule, PagerModule, ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

import 'zone.js';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);