Tech: Building Reusable Server-side Pagination for Ag-grid in Angular

Server-side Pagination Starter Kits

Khoi Bui
Webtips
4 min readAug 16, 2021

--

When it comes to picking the best grid components for UI, ag-grid will always be an indisputable pick by many developers. The fact that this library provides us the ability to customize its component and never disappoints us with its performance makes it one of the best component libraries out there. However, the free version of the library does not support server-side pagination out of the box which turns away large and medium app developers from using the library. Today we will learn how to build server-side pagination that works with ag-grid and Angular and how it can be reused across the app.

Prerequisites

  • Angular
  • ag-grid (Free version)

Implementation

The key secret about building server-side pagination for your grid is to provide your own implementation of datasource. Datasource is basically a callback method that is triggered whenever there are changes in the page number, page size, sorting, and filtering. We then encapsulate this logic inside an Angular directive in order to reuse the codes.

Grid APIs

First, we need to define some basic APIs below

  • getData: a callback method that handles remote data retrieval. This callback should provide several query parameters (page, pageNum, sort, etc.) and return an observable of data. Note that the returned observable must contain two items: the list of paginated data and the total records in the remote database.
  • getDataError: a callback method that handles exceptions when retrieving remote data.
  • gridApi: ag-grid’s APIs.

Anguar Directive

Next, we need to build a custom directive to implement our datasource. Our directive is called RemoteGridBindingDirective. It accepts an instance of RemoteGridApi as an input.

This directive will be attached to the ag-grid component to listen to the gridReady event. When gridEvent event gets triggered, the callback will provide us with gridApi object which we can use to set our custom datasource. After that, gridApi object will be emitted to the parent component.

See below for the implementation of datasource.

Our dataSouce object must implement getRows() function. This function should call getData() API from RemoteGridApi and handle success or error callbacks. Remember to pass the data list and total records as parameters in the success callback to update the grid.

Grid Container

The final step is to integrate the angular directive we declared previously to ag-grid component. Note that this component must implement RemoteGridApi in order to comply with our remote grid directive.

In this component we need to accomplish four things:

  • Update the gridOptions’ rowModelType to ‘infinite’ and declare cacheBlockSize and paginationPageSize for your grid.
  • Set the instance of this component to remoteGridBinding.
  • Implement getData() API.
  • Implement onGridReady() callback (optional).

This is the html template for our grid component. The component should apply the remoteGridBinding directive with the instance of RemoteGridApi.

See full demo here.

Conclusion

I hope this article will provide you some basic steps to get started with building server-side pagination with ag-grid. Feel free to post any questions in the comment below. Cheers!

--

--

Khoi Bui
Webtips

Front End architect, opensource contributor and investment enthusiast. New content posted every week.