iDisplayLength and fnServerData problem

iDisplayLength and fnServerData problem

DFDF Posts: 13Questions: 0Answers: 0
edited May 2012 in General
Because of expense, timing and collation issues (records are being collected from multiple commercial sources), my initial server data call returns 1-20 records out of what is usually a much larger group of records. For example, in a typical initial call, aoData will have 1-20 records, and iTotalRecords and iTotalDisplayRecords will be identical large values in the 100's.

Unless the numbers of elements in aoData exactly matches the inital iDisplayLength, sInfo display and pagination are wrong, regardless of the initial iDisplayLength, because It is expected that the number of records returned will be equal to iDisplayLength, unless the server has fewer records to return.

If I try to reset iDisplayLength dynamically to match the number of records of being returned and then call fnDraw in a callback routine, fnDraw requeries the server, which is not at all what I want.

How can I fix this?

Replies

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Hi,

    So to clarify my understanding - when you sat that the initial call returns 1-20, you mean, it might return a different number of records on each request?

    This isn't something that DataTables was designed to cope with, as I had always expected the number of records that are returned to exactly match that which is requested (with the exception being the 'last page' when there aren't enough records available - when iTotalRecords etc can come into play).

    Having said that, it is possible to do something like this:

    [code]
    $(document).ready(function() {
    var firstDraw = true;

    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "xhr.php",
    "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": function (json) {
    if ( firstDraw ) {
    oSettings._iDisplayLength = json.aaData.length;
    }
    fnCallback( json );
    }
    } );
    }
    } );
    } );
    [/code]

    What the code is doing is setting the internal display length parameter based on the data initially loaded on the first load.

    This isn't a particularly "clean" solution since it involves working with the internal settings externally, but there isn't currently an external API to allow what you are looking for (its a bit unusual :-) ).

    Does this sound like it would cover everything you need?

    Regards,
    Allan
  • DFDF Posts: 13Questions: 0Answers: 0
    Yes, I don't know how many records that I'll have initially and in all until the initial set of calculations,which are based on user input and triggered by a button click, are done on the server.

    I am in transit today but will try your suggestion, which I probably have to extend to every call except on the first page draw before the user has triggered anything, tonight (PDT, GMT -8) and let you know.

    Thanks.

    Doug
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Hi Doug,

    Ah okay - to the length might be variable on every return. So if you start on page 1, you might get 2 records, when click 'next' and you get 3 records, then click 'previous' and you might get 12 records? I think the internal logic should be able to cope with that (certainly it won't allow you to page back below row 0!), but do let me know how you get on with it.

    Allan
  • DFDF Posts: 13Questions: 0Answers: 0
    That worked!
    Thanks, Allan.
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Super - thanks for the feedback :-)

    Regards,
    Allan
This discussion has been closed.