最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't server side pagination works for my jquery datatables table? - Stack Overflow

programmeradmin2浏览0评论

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 plete 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.

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 plete 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.

Share Improve this question asked Feb 1, 2015 at 10:20 andruxandrux 2,9223 gold badges25 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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/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.

发布评论

评论列表(0)

  1. 暂无评论