I have a table loaded via AJAX and along with the table the pagination is also loaded through AJAX. The table contains a list of all users on my site limited to 30 at a time.
This is how I’m returning the response to the JavaScript from the controller:
$users = $this->users_m->get_users($type, $offset);
$num_rows = $this->users_m->user_stats($type);
$config['per_page'] = 30;
$config['num_links'] = 5;
$config['total_rows'] = $num_rows[0];
$this->pagination->initialize($config);
echo json_encode(array(
'users' => $users,
'pagination' => $this->pagination->create_links()
));
All is well except the pagination is never correct. The first time it is but on subsequent requests it is not.
When using the pagination class in a non AJAX page, the page number I click becomes the active one. Here page 1 is always active (surrounded by <strong> tags as opposed to being a link). Secondly the numbers never change. I get:
[1] [2] [3] [4] [5] [6] [>] [Last >]
every time. Even if I click last I get the same numbers back, it doesn’t change.
How to get the pagination class to work with AJAX?
Ok I came up with a working solution that I will post here in case anyone else has the same issue.
I used the pagination class found here:
http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/https://github.com/catchmyfame/PHP-Pagination-Class/blob/master/paginator.class.php
but modified it to work both as a CI library and also with my particular javascript. Create a new file called
Pagination_ajax.phpand put it in the same location as the default pagination class in
/system/libraries.This is the modified class:
You can do a diff between this and the original download to see exactly what I changed.
Controller code:
Model Code:
JQuery:
HTML
CSS
Hope someone finds this useful.