Customization with Ajax and server side pagination

Customization with Ajax and server side pagination

Fitz4591Fitz4591 Posts: 1Questions: 1Answers: 0
edited March 2017 in Free community support

Hi everyone,
I'm using a Java back-end with Spring Boot and Spring Data Rest. It exposes a REST API to access to data, and automatically do all the pagination stuff. When I make a GET call on the URI to get the data, I get an object in JSON HAL format like this:

{
  "_embedded:" {
    "user": [ ... ]
  },
  "page": {
    "size": 20,
    "totalElements": 150,
    "totalPages": 8,
    "number": 0
  }
}

As you can see the server gives me the size of the returned page, the total number of elements, and so on, but I can't (won't) choose how these elements are formatted.

Currently, using datatables with Ajax, I initialize the table "users" with something like:

$(function() {
        $('#users').DataTable({
            serverSide: true,
            ajax: {
                url: '/admin/api/data/user',
                dataSrc: '_embedded.user'
            },
            columns: [
                { data: 'num' },
                { data: 'date' },
                { data: 'email' }
            ]
        });
});

My questions are:
- how can I tell datatables where to get information about the number of pages, the size of pages ? Is there a way to tell datatables that the total number of elements will be returned by the Ajax request in "page.totalElements" ?
- my Ajax request also returns links that can be used to get a specific page, to sort the elements, or to search in all elements. Is there a way to let datatables know about this link so it can exploit them ?

This discussion has been closed.