I'm working on a bug fix right now for an application at work where the prior developer (since gone) didn't bother to paginate the data results on a page meant specifically for listing out data results.
This of course has reared it's ugly head as users are starting to see long running script errors in IE. This, combined with the sheer data volume size, is making web pages nearly useless.
Fast forward to my attempts to fix it and they've gone pretty well. The site is a .NET MVC 2 site that was developed using DataTables to add search/sort/paging functionality on the client. I'd just completed a similar task using jqGrid so figured this would be relatively straight forward. And it has been except one small problem. I cannot for the life of me get page links to generate.
A quick results view:
The results know that there are 2086 records in this query:
But paging links are not generated.
My action method is returning JSON via
return Json(new
{
param.sEcho,
iTotalRecords = totalRecords,
iTotalDisplayRecords = filteredContracts.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
where
param.sEcho = "1", iTotalRecords = 2086, iTotalDisplayRecords = 25, and aaData is the array result of data to display
To be thorough, he's the datatable initialize statement:
$("#tblToDoItems").dataTable({
'bServerSide': true,
'bProcessing': true,
'sAjaxSource': '/Home/GetContractList',
"bJQueryUI": true,
"bAutoWidth": false,
"bPaginate": true,
"sPaginationType": "full_numbers",
"iDisplayLength": 25,
/* make the first and last columns not sortable */
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [0, -1] }
]
});
Am I missing some setting that would prevent DataTables from properly generating pagination via server side data retrieval?
Your iTotalDisplayRecords is equal to 25, so datatables think that there are only 25 contracts on the server side and second page is not needed because all of them are already shown on the current page. This is comon mistake - if you take a look at the JQuery MVC tutorial section Implementation of server-side paging you will see that there are three numbers:
If you put all.Count insteadof the result.Count into the iTotalDisplayRecords DataTables will show paging. iTotalDisplayRecords and iTotalRecords are used to show message "Showing 1 to 25 of iTotalDisplayRecords (iTotalRecords in total)"
If iTotalDisplayRecords is equal to 25, DataTables will show message "Showing 1 to 25 of 25 (iTotalRecords in total)", and assume that there is no page 2; hence, paging will be disabled, as in your example.
Jovan
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With