DataTables Bug: sAjaxDataProp breaks pagination
DataTables Bug: sAjaxDataProp breaks pagination
jcrawford
Posts: 172Questions: 0Answers: 0
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.
[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.
This discussion has been closed.
Replies
[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
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.
Allan