Paging Server Side Processing / XML

Paging Server Side Processing / XML

ArtensArtens Posts: 2Questions: 0Answers: 0
edited January 2018 in Free community support

Hello there,

I have a problem which I can't figure out.

I'm using datatables server side processing to get data from a postgresql database. It works great but there is a problem with the pagination.

I display 25 row for each page, when i switch from page 1 to 2 there is no problem, but the count of all my data is 50, so I want the pagination to stop at 2 but datatable display page 3/4/5 which are empty.

So here is my question, how can I have the right pagination ?

Here is the code of my datatable creation :

dataTableElement = $('#tablename').DataTable({
        "serverSide": true,
        "info": false,
        "iDisplayLength": 25,
        "ajax" : {
            "url" : "/",
            "type" : "POST",
            "data" : {
                request : 'request',
                element : element,
                page : page,
                historicSearch : historicSearch,
                "source" : 'datatable'
            },
            "dataSrc" : "objects",
            "dataFilter" : XML2JSON
        },
        "language": {
            "lengthMenu": "Afficher _MENU_ éléments par page",
            "zeroRecords": "Aucun élément.",
            "infoEmpty": "Rien à afficher.",
            "search": "Rechercher : ",
            "paginate": {
              "previous": "Précédent",
              "next": "Suivant"
            },
        },
        "columnDefs": [
            {"name": "0", "targets": 0},
            {"name": "1", "targets":1},
            {"name": "2", "targets":2},
            {"name": "3", "targets":3},
            {"name": "4", "targets":4},
            {"name": "5", "targets": 5},
            {"name": "6", "targets": 6},
            {"name": "7", "targets": 7},
            {"name": "8", "targets": 8},
            {"name": "9", "targets": 9},
            {"name": "10", "targets": 10},
            {"name": "11", "targets": 11},
            {"name": "12", "targets": 12},
            {"name": "13", "targets": 13}
        ],
        conditionalPaging: true,
        "columns": [
            {data: "0", title: "0"},
            {data: "1", title: "1"},
            {data: "2", title: "2"},
            {data: "3", title: "3"},
            {data: "4", title: "4"},
            {data: "5", title: "5"},
            {data: "6", title: "6"},
            {data: "7", title: "7"},
            {data: "8", title: "8"},
            {data: "9", title: "9"},
            {data: "10", title: "10"},
            {data: "11", title: "11"},
            {data: "12", title: "12"},
            {data: "13", title: "13"}
        ],
        "order": [[ order, "asc" ]]
    });

I'm using XML to get the data which will be display like that :

<?xml version="1.0" encoding="utf-8"?>
<response>
    <header>
        <timestamp>2018-01-15 11:35:24</timestamp>
        <request>request</request>
        <element>element</element>
        <template>template</template>
        <datetime>15/01/2018 11:35:24</datetime>
        <exectime>0.035</exectime>
    </header>
    <data>
        <previousPage>previouspage</previousPage>
        <recordsTotal>2</recordsTotal>
        <objects pkey="id" total="2">
            <object id="1">
                <attr name="0">0</attr>
                <attr name="1">1</attr>
                <attr name="2">2</attr>
                <attr name="3">3</attr>
                <attr name="4">4</attr>
                <attr name="5">5</attr>
                <attr name="6">6</attr>
                <attr name="7">7</attr>
                <attr name="8">8</attr>
                <attr name="9">9</attr>
                <attr name="10">10</attr>
                <attr name="11">11</attr>
                <attr name="12">12</attr>
                <attr name="13">13</attr>
                <etatService>0</etatService>
            </object>
            <object id="3">
                <attr name="0">0</attr>
                <attr name="1">1</attr>
                <attr name="2">2</attr>
                <attr name="3">3</attr>
                <attr name="4">4</attr>
                <attr name="5">5</attr>
                <attr name="6">6</attr>
                <attr name="7">7</attr>
                <attr name="8">8</attr>
                <attr name="9">9</attr>
                <attr name="10">10</attr>
                <attr name="11">11</attr>
                <attr name="12">12</attr>
                <attr name="13">13</attr>
                <etatService>0</etatService>
            </object>
        </objects>
    </data>
</response>

The XML2JSON function transform XML into JSON, so I can get every parameters I want, like "recordsTotal" which I think is the key of the problem.

Here is the display of JSON I have :

{
   "recordsTotal":"2",
   "objects":[
      {
         "0":"0",
         "1":"1",
         "2":"2",
         "3":"3",
         "4":"4",
         "5":"5",
         "6":"6",
         "7":"7",
         "8":"8",
         "9":"9",
         "10":"10",
         "11":"11",
         "12":"12",
         "13":"13"
      },
      {
         "0":"0",
         "1":"1",
         "2":"2",
         "3":"3",
         "4":"4",
         "5":"5",
         "6":"6",
         "7":"7",
         "8":"8",
         "9":"9",
         "10":"10",
         "11":"11",
         "12":"12",
         "13":"13"
      }
   ]
}

So what can I do to have the right pagination in my page ?

Many regards,

Artens.

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi Artens,

    The problem is with the returned data, it's not quite right for several reasons:

    1. your "objects" array should be a "data" array
    2. you correctly specified "recordsTotal", but you also need the mandatory field "recordsFiltered" - for you, both should be the same value.
    3. "draw" is also a mandatory field, so that needs to be specified (this will be "1" for you)

    This page on server-side processing will help.

    Hope that helps,

    Cheers,

    Colin

  • ArtensArtens Posts: 2Questions: 0Answers: 0

    Thanks for your answer, my problem is solved :smile:

This discussion has been closed.