iDisplayLength and fnServerData problem
iDisplayLength and fnServerData problem
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?
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?
This discussion has been closed.
Replies
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
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
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
Thanks, Allan.
Regards,
Allan