How to set recordsTotal from a server side REST?

How to set recordsTotal from a server side REST?

moyomoyo Posts: 4Questions: 1Answers: 0

Hello,

I have a POST-REST service from where I get the data, and I would like to know how to set the recordsTotal and recordsFiltered value.
All the REST responses object have a three attributes structure, and it couldn't be modified (I mean, I cant add recordsTotal and recordsFiltered to the main structure. Maybe at the "info" attribute... )
- code
- message
- info <here goes the data for the DataTable>

Here is a piece of my code:

        "serverSide": true,
        ajax: {
           async: true,
           url: "/restEndPoint",
           contentType: "application/json",
           type: "POST",
           dataSrc: "info"
           timeout: 120000,
           data: function ( d ) {
              return JSON.stringify( dataTableFilter );
           },
        },
        preDrawCallback: function(){
           # way to pass the 'start' and 'len' value to the server
            dataTableFilter.start = this.api().page.info().start;
            dataTableFilter.length = this.api().page.len();
        },

How can I set the recordsTotal and recordsFiltered value without modify de REST Response?

pd.:

I tried to use "initComplete", but even though these values get modify, it don't see it in the view

        initComplete: function(settings, json){
            settings._iRecordsTotal = 4; #or json.length
            settings._iRecordsDisplay = 4; #or json.length
            settings.iDraw = 1;
        },

Thanks.

Answers

  • kthorngrenkthorngren Posts: 21,375Questions: 26Answers: 4,957

    Is the REST API returning all the rows or is it returning just one page worth of data? If its all rows then you don't need "serverSide": true, and can remove this line.

    Kevin

  • moyomoyo Posts: 4Questions: 1Answers: 0

    kthorngren, thanks for your answer

    it's a large table, so it's need to be managed in server side.

  • moyomoyo Posts: 4Questions: 1Answers: 0
    edited June 2020

    SOLVED

    and in a very easy way ...
    Remembering the "RestResponse" object looks like:
    - code
    - message
    - info

    just add this few lines at ajax call:

               dataSrc: function(response){
                   response.recordsTotal = response.info.length;
                   response.recordsFiltered = response.info.length; # or data from "info" atributte
                   response.draw = 1;
                   return response.info;
               }
    

    Ed.: So, putting all together

    dataTableFilter = {};
    ...
    "serverSide": true,
    ajax: {
    async: true,
    url: "/restEndPoint",
    contentType: "application/json",
    type: "POST",
    dataSrc: function(response){
    response.recordsTotal = response.info.length;
    response.recordsFiltered = response.info.length; # or data from "info" atributte
    response.draw = 1;
    return response.info;
    },
    timeout: 120000,
    data: function ( d ) {
    return JSON.stringify( dataTableFilter );
    },
    },
    preDrawCallback: function(){
    # way to pass the 'start' and 'len' value to the server
    dataTableFilter.start = this.api().page.info().start;
    dataTableFilter.length = this.api().page.len();
    },
    ...

  • kthorngrenkthorngren Posts: 21,375Questions: 26Answers: 4,957
    edited June 2020

    If you enable server side processing then Datatables expects to use the protocal described here which it seems you are trying to emulate.

    How does your REST API control the number of rows returned? Maybe this?
    return JSON.stringify( dataTableFilter );

    Unless your REST API incorporates some sort of paging mechanism I don't think enabling server side processing is going to help. Your REST API would would need the ability to respond only the number of records for the page. For example if the data has 1000 rows and you go to page 2 with a page length of 10 then your REST API would need to return records 10-19 only. If needed sorting and searching wil also need to be incorporated into the REST API response. Is your REST API capable of this?

    Kevin

  • moyomoyo Posts: 4Questions: 1Answers: 0

    Correct, kthorngren

    The preDrawCallback function initializes the "dataTableFilter" values ​​that will be passed as a @RequestBody object.

    The main problem was at the return, where I didn't see how to pass the "records" values without modify the Response "3-attributes" structure.
    Once it could be managed at "dataSrc" (ajax), I could work with the content of the "info" attribute wihtout alter the main structure.

This discussion has been closed.