Pagination doesn't seem to work with server side data
Pagination doesn't seem to work with server side data
ajackson
Posts: 1Questions: 0Answers: 0
I've run into a problem that I just can't seem to get my head around. My application is pulling messages into a datatable. I set a lot of options to false to narrow the nature of the problem. Below is my code that initializes the data table. My presumption was that telling it to do server side processing would naturally mean that paging would require a callout to retrieve the next page.
$('#message-table').dataTable({
"sAjaxSource" : 'Services/Messages?Channel=' + channel,
"bProcessing" : false,
"bServerSide" : true,
"bFilter" : false,
"bJQueryUI" : true,
"bLengthChange" : false,
"bAutoWidth" : false,
"sEmptyTable" : "No messages found",
"iDisplayLength" : 20,
"aoColumnDefs": [
...
],
});
What I find is that I get my table populated. In the inform portion it says "Showing 1 to 20 of 20 entries (filtered from 24 total entries)". So far, so good, it's showing the first 20 entries and has a clear idea that there are 24 total entries. The paging buttons for prev and next are there. When I click them, nothing happens. So I set a few breakpoints to see what was really going on under the hood.
The buttons are hooked up to _fnPageChange() where action is "prev" or "next" for these buttons. When next is pressed, it goes down the path, but the following occurs ...
/* Make sure we are not over running the display array */
if ( oSettings._iDisplayStart + oSettings._iDisplayLength < oSettings.fnRecordsDisplay() )
{
oSettings._iDisplayStart += oSettings._iDisplayLength;
}
Well, fnRecordsDisplay() happens to return 20. Same value as _iDisplayStart + _iDisplayLength. This means that I'm "constrained" within the window of records that were pulled down from the server during the initial ajax call. Clearly, my intent was to see messages 21-24.
FYI, is the rough response from my server call.
{"sEcho":1,"iTotalRecords":24,"iTotalDisplayRecords":20,"aaData":[...]}
I think that's all that it's looking for...
Thanks in advance for your help.
Aaron
$('#message-table').dataTable({
"sAjaxSource" : 'Services/Messages?Channel=' + channel,
"bProcessing" : false,
"bServerSide" : true,
"bFilter" : false,
"bJQueryUI" : true,
"bLengthChange" : false,
"bAutoWidth" : false,
"sEmptyTable" : "No messages found",
"iDisplayLength" : 20,
"aoColumnDefs": [
...
],
});
What I find is that I get my table populated. In the inform portion it says "Showing 1 to 20 of 20 entries (filtered from 24 total entries)". So far, so good, it's showing the first 20 entries and has a clear idea that there are 24 total entries. The paging buttons for prev and next are there. When I click them, nothing happens. So I set a few breakpoints to see what was really going on under the hood.
The buttons are hooked up to _fnPageChange() where action is "prev" or "next" for these buttons. When next is pressed, it goes down the path, but the following occurs ...
/* Make sure we are not over running the display array */
if ( oSettings._iDisplayStart + oSettings._iDisplayLength < oSettings.fnRecordsDisplay() )
{
oSettings._iDisplayStart += oSettings._iDisplayLength;
}
Well, fnRecordsDisplay() happens to return 20. Same value as _iDisplayStart + _iDisplayLength. This means that I'm "constrained" within the window of records that were pulled down from the server during the initial ajax call. Clearly, my intent was to see messages 21-24.
FYI, is the rough response from my server call.
{"sEcho":1,"iTotalRecords":24,"iTotalDisplayRecords":20,"aaData":[...]}
I think that's all that it's looking for...
Thanks in advance for your help.
Aaron
This discussion has been closed.
Replies
iTotalDisplayRecords should be same as iTotalDisplay unless you have enter some keys in searchbox. your sql query needs limit and offset values and those values come from iDisplayStart and iDisplayLength.
If you are using PHP5.2+, i advice you to use Ignited Datatables which is a very flexible server-side library.
https://github.com/n1crack/IgnitedDatatables-native-php-version
http://www.datatables.net/forums/discussion/comment/21293#Comment_21293