Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ag grid Server side pagination

I'm trying to implement a server side pagination in ag-Grid where I'll make a SOAP call each time I click on the next/previous button. I have already implemented the function with the specific page number so I can retrieve my row data and pass it to the Grid. Any good examples on how to do that? Thanks in advance.

like image 691
Steeve Avatar asked Nov 16 '17 20:11

Steeve


People also ask

How do you implement server-side pagination in Ag grid?

Example: Server-side Pagination Pagination is enabled using the grid option pagination=true . A pagination page size of 10 (default is 100) is set using the grid option paginationPageSize=10 . The number of rows returned per request is set to 10 (default is 100) using cacheBlockSize=10 .

What is server-side pagination?

Server-side pagination requests each page individually loaded upon navigation. The server needs to support pagination, and then the client-side is able to request data sets with specified page size, page index, and possibly sorting orders and filtering criteria.

What is pagination in Ag grid?

Pagination allows the grid to paginate rows, removing the need for a vertical scroll to view more data. To enable pagination set the grid property pagination=true . Example: Client Paging.


1 Answers

After digging ag-grid Library for the whole day , I finally found the solution.

First Lets include the following options in our GridOptions;

GridOptions :

  gridOptions: GridOptions = {
    pagination: true,
    rowModelType: 'infinite',
    cacheBlockSize: 20, // you can have your custom page size
    paginationPageSize: 20 //pagesize
  };

GridReady CallBack

After the Grid is ready, Set a datasource e.g

onGridReady(params: any) {
    this.gridApi = params.api;
    this.gridApi.setDatasource(this.dataSource) // replace dataSource with your datasource
  } 

Data Source Object : dataSource object is called by ag-grid when you go to next page and thus fetches data.

getRows functions provides you with start and end index of the particular page.

params.successCallback : It takes two arguments, first the data related to page and second is totalRecords. Ag-grid uses the second parameter to decide total pages.

dataSource: IDatasource = {
    getRows: (params: IGetRowsParams) => {

      // Use startRow and endRow for sending pagination to Backend
      // params.startRow : Start Page
      // params.endRow : End Page

      //replace this.apiService with your Backend Call that returns an Observable
      this.apiService().subscribe(response => {

        params.successCallback(
          response.data, response.totalRecords
        );

      })
    }
  }

Example Api Service : This is an example api service that i have used

apiService() {
    return this.httpclient.get('https://raw.githubusercontent.com/ag-grid/ag-grid/master/packages/ag-grid-docs/src/olympicWinners.json')
  }

I have uploaded this example on GitHub.

like image 112
Renil Babu Avatar answered Sep 19 '22 14:09

Renil Babu