Pagination and "Showing 0 to 0 records" message
Pagination and "Showing 0 to 0 records" message

I have downloaded the DataTables 1.10.20 version and could not get the datatables to display the "Showing # to # of #" message properly. It was always coming up with a zero value.
I found that the json results from the server using MCV C#, which worked fine in a prior datatables release using VB.net, was sending information back as a JSON result set with all the information set up as
** ContentEncoding: null
ContentType: null
Data: { draw = 1, recordsTotal = 9821,
recordsFiltered = 9821,
data = {System.Collections.Generic.List<<>f__AnonymousType6<int, string, string, string>>} }
JsonRequestBehavior: AllowGet
MaxJsonLength: null
RecursionLimit: null
**
MVC code
** var results = Json(new { draw=vliDraw, recordsTotal=vlirecordsTotal, recordsFiltered=vlirecordsFiltered, data=lst }, JsonRequestBehavior.AllowGet);
return Json(results, JsonRequestBehavior.AllowGet);
**
Because "data" is inside the "DATA" object, the datatables function was not finding the values.
I had to modify the below function to check for the json.DATA first and then return the information based on whether it found the object or not. This at least is now returning both the records and the pagination information.
function _fnAjaxUpdateDraw ( settings, json )
{
// v1.10 uses camelCase variables, while 1.9 uses Hungarian notation.
// Support both
var compat = function (old, modern) {
** if (json.Data !== undefined) {
return json.Data[old] !== undefined ? json.Data[old] : json.Data[modern];
}
else {
return json[old] !== undefined ? json[old] : json[modern];
}**
};
var data = _fnAjaxDataSrc( settings, json );
var draw = compat( 'sEcho', 'draw' );
var recordsTotal = compat( 'iTotalRecords', 'recordsTotal' );
var recordsFiltered = compat( 'iTotalDisplayRecords', 'recordsFiltered' );
if ( draw ) {
// Protect against out of sequence returns
if ( draw*1 < settings.iDraw ) {
return;
}
settings.iDraw = draw * 1;
}
_fnClearTable( settings );
settings._iRecordsTotal = parseInt(recordsTotal, 10);
settings._iRecordsDisplay = parseInt(recordsFiltered, 10);
for ( var i=0, ien=data.length ; i<ien ; i++ ) {
_fnAddData( settings, data[i] );
}
settings.aiDisplay = settings.aiDisplayMaster.slice();
settings.bAjaxDataGet = false;
_fnDraw( settings );
if ( ! settings._bInitComplete ) {
_fnInitComplete( settings, json );
}
settings.bAjaxDataGet = true;
_fnProcessingDisplay( settings, false );
}
Replies
It sounds like you just need to use
ajax.dataSrc
, rather than modify the library code.Colin