DataTables Bug: sAjaxDataProp breaks pagination

DataTables Bug: sAjaxDataProp breaks pagination

jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
edited November 2011 in Bug reports
In one of my data tables I was attempting to set the following option

[code]
"sAjaxDataProp": "output.aaData",
[/code]

Everything worked fine except for pagination. The pagination showed all page numbers as being page-able when there was NO data in the table. Here is the output from the ajax

[code]
{"output":{"sEcho":2,"iTotalRecords":"0","iTotalDisplayRecords":"0","aaData":[]}}
[/code]

here is a screen shot of my table in use: http://www.josephcrawford.com/pics/sAjaxPropPaginationBug.png

and here is the table initialization code

[code]
$(document).ready( function() {
var oTable = $('#resourcelog').dataTable( {
"bFilter": false,
"bServerSide": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"sAjaxSource": "/admin/pstoolresourceajax/?resource="+resource+"&start_date="+default_start_date+"&end_date="+default_end_date,
"aaSorting": [[ 1, "desc" ]],
"oLanguage": {
"sZeroRecords": "No transactions found."
},
"sAjaxDataProp": "output.aaData",
"sDom": '<"H"Tfr>t<"F"ip>',
"oTableTools": {
"sSwfPath": "/admin-assets/TableTools.swf",
"aButtons": [
"copy", "print",
{
"sExtends": "collection",
"sButtonText": "Save",
"aButtons": [ "csv", "xls", "pdf" ]
}
]
},
"fnServerData": function( sSource, aoData, fnCallback) {
$.getJSON(sSource, aoData, function(json) {
if(jQuery.isEmptyObject(json)) location.href = '/admin/login/';
else fnCallback(json);
});
},
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
/* numbers less than or equal to 0 should be in red text */
if(aData[2] != undefined) {
if ( aData[2].indexOf('D') == 0 ) {
jQuery('td:eq(2)', nRow).addClass('transactionDebit');
} else {
jQuery('td:eq(2)', nRow).addClass('transactionCredit');
}
}
return nRow;
},
} );
});
[/code]

Now keep in mind removing the sAjaxDataProp option and making the ajax just output the json there is no issue with the pagination.

Replies

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    The key here is this:

    [code]
    {"output":{"sEcho":2,"iTotalRecords":"0","iTotalDisplayRecords":"0","aaData":[]}}
    [/code]

    sAjaxDataProp only effects where the data array is, not sEcho, iTotalRecords etc - they all need to be at the "top level" of the returned JSON object. I can see that it might be useful to do as you have done, but I'm afraid that's not the way it works at the moment.

    Allan
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    Allan,

    Thanks for the information, that explains why it is not working :) Since it will not work this way I will have to revert to the other way of using DataTables.
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Another option is to use fnServerData to modify the JSON that has been returned from the server (i.e. copy the sEcho etc parameters onto the "top level" of the return. But generally speaking, the return format for DataTables is tightly defined ( http://datatables.net/usage/server-side ), its the data options that provide a wide range of flexibility.

    Allan
This discussion has been closed.