简体   繁体   中英

Why doesn't server side pagination works for my jquery datatables table?

I'm trying to make server side pagination work but it just doesn't work. I have been looking all over for a solution to my problem but everything I have found is pretty much the same I have, which does not work.

I'm probably missing something obvious here, I haven't used datatables that good and I find its instructions and examples a bit too confusing.

Here's my Javascript code - it's being printed from inside a PHP file as inline Javascript code, so I had to replace PHP parts for brevity:

var display_start = 1;
var display_length = 10;

$( '#andrux' ).dataTable({
    'sPaginationType': 'full_numbers',
    'processing': true,
    'serverSide': true,
    'bSort': false,
    'ajax': '/bp-subgroups.php?group=3&iDisplayStart=' + display_start + '&iDisplayLength=' + display_length
});

According to the samples I've seen, I shouldn't even append the iDisplayStart or iDisplayLength variables to my request, but that's the only way I don't get the complete load of records into my table.

I know what happens in this scenario, I always send 1 and 10 as display start and display length in my request, I guess the question should be, how do I keep those updated depending on the page I'm at? Shouldn't they be automatically updated with each request?

Here's the output I'm sending from my PHP code:

$output = array(
    "sEcho" => intval( $_GET['sEcho'] ),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iTotal,
    "aaData" => array()
);

Of course aaData is being loaded with my rows, I only include the above for brevity, plus, I can see the data just fine, I just can't make it paginate.

Anyone with experience that can see what I'm doing wrong?

This is already driving me nuts, but I don't want to use another plugin for this, I don't like giving up!

Thanks everyone.

Well, since no one gave this a shot I just had to exhaust all my resources and find a solution myself.

The answer was here: http://www.datatables.net/examples/data_sources/server_side.html

Inside the "Server-side script" tab, at the very bottom, they include the SSP class. Since I'm using WordPress here, I wrongly assumed I wouldn't need that class, because my queries are via $wpdb or other WP core functions, plus, I was reading all the documentation online and didn't even think of looking into that class - why would I, if I was following the rest of the instructions that were supposed to work.

Long story short, I did some modifications to the SSP class to suit my needs, then used pretty much the same setup the example pages have, and finally got my datatables paginating correctly.

Just as reference, I was trying to return an array like the following to datatables:

array(
    "sEcho" => intval( $_GET['sEcho'] ),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iTotal,
    "aaData" => $my_data
);

When it turned out I need to return the array like this:

array(
    "draw"            => intval( $request['draw'] ),
    "recordsTotal"    => intval( $recordsTotal ),
    "recordsFiltered" => intval( $recordsFiltered ),
    "data"            => $my_data
);

Probably it's just me, but datatables although a nice working plugin, really needs more work on the documentation. I found some parameters were found one way in one place and another way elsewhere - just like the example above.

在我的情况下,我没有传递“draw”的值,请尝试传递此值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM