Data tables paginate bug

Data tables paginate bug

joao.r.silvajoao.r.silva Posts: 2Questions: 0Answers: 0
edited June 2010 in Bug reports
Hi Everyone.

I just had a problem with the pagination when in server side.
The problem was that although I was sending the total number of records correctly but pagination would think I had just 1 page, thus not enabeling.

Looking thought the code I saw that the value used to make proper pagination only works when you send all the records of the table, therefore I corrected the problem like this.

At the top of the function I added the following code
function _fnPageChange ( oSettings, sAction )
{
var effectiveRecords =0

if ( oSettings.oFeatures.bServerSide ) {
effectiveRecords=oSettings.fnRecordsTotal();
} else {
effectiveRecords=oSettings.fnRecordsDisplay();
}

In the same function I changed the code for Next and Last buttons as in:

else if ( sAction == "next" )
{
if ( oSettings._iDisplayLength >= 0 )
{
/* Make sure we are not over running the display array */
if ( oSettings._iDisplayStart + oSettings._iDisplayLength < effectiveRecords )
{
oSettings._iDisplayStart += oSettings._iDisplayLength;
}
}
else
{
oSettings._iDisplayStart = 0;
}
}
else if ( sAction == "last" )
{
if ( oSettings._iDisplayLength >= 0 )
{
var iPages = parseInt( (effectiveRecords-1) / oSettings._iDisplayLength, 10 ) + 1;
oSettings._iDisplayStart = (iPages-1) * oSettings._iDisplayLength;
}
else
{
oSettings._iDisplayStart = 0;
}
}

Also I changed code in the function:
"fnUpdate": function ( oSettings, fnCallbackDraw ) {
...
var iPages=0;
if ( oSettings.oFeatures.bServerSide ) {
iPages = Math.ceil((oSettings.fnRecordsTotal()) / oSettings._iDisplayLength);
} else {
iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength);
}

Then it works fine.
I don't know what people want to do to correct the problem and I think my code could be a little more elegant. I'm sending this just to tell what I deeded to do to correct it and help you guys to fix this bug.

Thanks to all

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    I'm not sure I understand. As this page ( http://datatables.net/usage/server-side ) notes, you need to send back:

    iTotalRecords - Total records, before filtering (i.e. the total number of records in the database)

    iTotalDisplayRecords - Total records, after filtering (i.e. the total number of records after filtering has been applied - not just the number of records being returned in this result set)

    aaData - 2D array of data to be displayed, for this 'page' only.

    So doesn't this do what you are looking for already?

    Allan
  • joao.r.silvajoao.r.silva Posts: 2Questions: 0Answers: 0
    Thnaks for the repply allan:

    The answer to your last question is no.
    I my case the iTotalDisplayRecords in the pagination code is somehow ignored by the current dataTables code.
    Here is a sample of my jason code that is returned to the DataTable.
    [code]
    {"sEcho": 1, "iTotalRecords": 44, "iTotalDisplayRecords": 10, "aaData": [["1","login","Sim"],["2","index","Sim"],["3","lojas","Sim"],["4","login/logout","Sim"],["5","gateway","Sim"],["6","pagamentos","Sim"],["7","formaspagamentoloja","Sim"],["8","formaspagamento","Sim"],["9","faturassituacoes","Sim"],["10","faturas","Sim"],]}
    [/code]

    In fact the databales code knows my request has a total of 44 records. For example in the show rows combobox it works fine, but the pagination doesn't. With this code the pagination is still disabled because the datatables thinks it has just one page.

    This appens because on dataTables code line 485.
    [code]
    var iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength);
    [/code]

    The number of pages is calculated using fnRecordsDisplay and not fnRecordsTotal.

    Thanks
    Joao
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    iTotalRecords and iTotalDisplayRecords should only be different when you have a filter applied. DataTables can easily do a aaData.length to find out how many records are in the display array. iTotalDisplayRecords notes how many records can be displayed after filtering, and iTotalRecords before filtering. This is explained here: http://datatables.net/usage/server-side

    Allan
  • bilbodigibilbodigi Posts: 18Questions: 0Answers: 0
    Hi
    I have the same problem.
    In a table with 25 rows and show 10 rows selected the pagination (i'm using full pagination) will only show one page.
    The button next and last are disabled.
    Picture http://screencast.com/t/NjMyZjgz

    [code]
    oaTable = $('#adminData').dataTable({
    "aaSorting": [[0,'desc'], [1,'desc']],
    "bJQueryUI": true,
    "bProcessing": true,
    "bServerSide": true,
    "bAutoWidth": false,
    "sPaginationType": "full_numbers",
    "sAjaxSource": "includes/functions/getAdminInfo.func.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    /* Add some extra data to the sender */
    aoData.push( oaTable.advancedSearch );
    $.getJSON( sSource, aoData, function (json) {
    /* Do whatever additional processing you want on the callback, then tell DataTables */
    fnCallback(json)
    } );
    },
    //var oaSettings = oaTable.fnSettings();

    "fnDrawCallback": function ( oaSettings ) {
    setupAdminTDClick(oaSettings);
    },
    "aoColumns": [
    {
    "bVisible": false
    },
    {
    "sWidth": "50px",
    "sClass":"setTDPadding"
    },
    {
    "sWidth": "200px",
    "sClass":"setTDPadding"
    },

    {
    "sWidth": "200px",
    "sClass":"setTDPadding"
    },

    {
    "sWidth": "60%",
    "sClass":"setTDPadding"
    },

    {
    "sWidth": "24px",
    "bSortable": false,
    "sClass":"mySel"
    }
    ]
    });
    [/code]
  • sunmit9sunmit9 Posts: 19Questions: 1Answers: 0
    Hi, I have a problem with paginate too. My code looks like this:
    [code]
    var oTable = $('#myTable').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "query/getQuery",
    "bPaginate" : true,
    "bLengthChange": true,
    "bScrollCollapse" : true,
    "iDisplayLength" : 20,
    "bFilter" : true,
    "bJQueryUI" : true,
    "sPaginationType" : "full_numbers",
    "sSearch": "Search",
    "sDom": '<"H"<"projectTeamTools">lrft>' // line A
    });

    $("div.projectTeamTools").html('Organize by Project Teams: ${projectTeam.projectName}');
    [/code]

    The dom is a custom div I have created to display a drop-down box.
    The datatable does not show the paginate tool. It considers that I have passed only one page although my displayLength is 20 and the records fetched are greater than 20, it shows only 20 records.

    But when I comment out the 'line A', the paginate tools are visible.
    Is this wht problem with the functionality or the css?
    Please help!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    > "bServerSide": true

    Most likely your server-side script isn't returning the required values for iTotalRecords and iTotalDisplayRecords: http://datatables.net/usage/server-side .

    Can you use the debugger so I can confirm if this is the case ( http://debug.datatables.net ).

    Allan
  • sunmit9sunmit9 Posts: 19Questions: 1Answers: 0
    edited May 2012
    Thanks for the response. I have uploaded the data Table data on the debugger. But I dont think there is a problem with the iTotalRecords and iTotalDisplayRecords. Because, even if I manually provide values to the two parameters, the paginate tool is not displayed. And even if the values returned by the server are different from what expected, the data table is supposed to display the paginate tool, right? It may be disabled in that case.
    But in my case, it doesn't even display the tool when I display my sDom. But it displays the paginate if i disable my sDom. Is this the problem with css. If yes, could you please help me with it.

    Also, I am facing a problem regarding the sDom. Could you please look in here (http://www.datatables.net/forums/discussion/9862/cannot-view-the-paginate-tool-at-the-bottom-of-the-table#Item_1).

    Regards,
    Sunmit.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    > I have uploaded the data Table data on the debugger

    And what was the debug code it gave you? Without that I can't access it :-).

    Allan
  • sunmit9sunmit9 Posts: 19Questions: 1Answers: 0
    edited May 2012
    Sorry. :) The debug code is 'omogef'.

    Sunmit
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    You are using an old version of DataTables (1.7.5). Can you update to the latest (1.9.1) and run the debugger over it again. Alternatively I'd need a link to the page.

    Allan
  • sunmit9sunmit9 Posts: 19Questions: 1Answers: 0
    edited May 2012
    Is it only because of the old version I'm using that I am getting such errors? Because upgrading to a new Version of DataTables on my project can affect a lot of other things.

    ~Sunmit
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    edited May 2012
    I don't know I'm afraid. I'd need a link to see what is currently going wrong. The debugger is deigned to work with 1.8+ (it partly works with 1.7, but since 1.7 doesn't have a lot of the features the newer versions have, there is less information available).

    So as I say, without a link I can't say what is wrong with it.

    Allan
  • sunmit9sunmit9 Posts: 19Questions: 1Answers: 0
    I'm sorry I can't provide the link due to some security reasons. I mean, I work for a software company and I'm not supposed to. But are you sure that the problem is in the older version. Maybe I can upgrade to newer. But wont that affect my current code? I sorry i'm asking for too much!

    ~Sunmit
  • sunmit9sunmit9 Posts: 19Questions: 1Answers: 0
    edited May 2012
    I guess I got the answer. I didn't know more about the sDom syntax. After searching, I figured that I had forgot to include an 'p' i.e. paginate in my sDom. So now, my sDom looks like:

    [code]"sDom": '<"H"<"projectTeamTools">lfrtp>'[/code]

    Also, could you please help me on a problem I am facing here (http://www.datatables.net/forums/discussion/9862/cannot-view-the-paginate-tool-at-the-bottom-of-the-table#Item_1)

    ~Sunmit
This discussion has been closed.