info@ektanjali.com +91 99276-99286

CakePHP 2.x User Management Basic Plugin Version 1.7.x

Ajax Pagination

I have done some changes for normal pagination to ajax pagination so now you have both options. Either you can use normal pagination or can use ajax pagination.

How to use Ajax pagination in Cake Php
1. set pagination variable in your controller for e.g. in UsersController.php

public $paginate = array(
            'limit' => 25
        );


2. Now in any function you should get results from Database in following way for e.g. in index function

$this->paginate = array('limit' => 10, 'order'=>'User.id desc');
$users = $this->paginate('User');

$this->set('users', $users);
if($this->RequestHandler->isAjax()) {
    $this->layout = 'ajax';
    $this->render('/Elements/all_users');
}


3. Now your View file code is conditional code for reducing the duplicate code. I mean View code is divided in two parts one for ajax updation and other for normal request. Confused ?? I know it is little bit complex don't worry I am going to explain in easy way.
Just open the index file, location is yourapp/app/Plugin/Usermgmt/View/Users/index.ctp

This code will never execute on ajax calls because there is no need.

I have added element call in this file

<?php echo $this->element('Usermgmt.all_users'); ?>

4. Now find the element file all_users.ctp in Elements directory

Now find the div with id "updateUserIndex" got it?? The section inside this div will update on every ajax call.

and in this file you should add paginator element in following way for e.g. in all_users.ctp before the results table

<?php echo $this->element('Usermgmt.paginator', array("useAjax" => false"updateDivId" => "updateUserIndex")); ?> where
'paginator' is a paginator element, location is yourapp/app/Plugin/Usermgmt/View/Elements/paginator.ctp.
'useAjax' is true or false. If you want to use ajax pagination set it true otherwise set it false.
'updateDivId' is id of main div. The contents of this div will update on every ajax call. I have explained this in step 4 above. In index.ctp I have set this id "updateUserIndex" you can change this according your wish.

5. Now in element file you should add pagination element in following way for e.g. in all_users.ctp after the results table

<?php if(!empty($users)) { echo $this->element('Usermgmt.pagination', array("totolText" => "Number of Users")); } ?> where
$users is result variable, set from controller's index function.
'pagination' is a pagination element, location is yourapp/app/Plugin/Usermgmt/View/Elements/pagination.ctp.
'totolText' is a total text to be displayed on pagination bar.

For more information see
Users Controller yourapp/app/Plugin/Usermgmt/Controller/UsersController.php
Index View yourapp/app/Plugin/Usermgmt/View/Users/index.ctp
Index View yourapp/app/Plugin/Usermgmt/View/Elements/all_users.ctp
Paginator Element yourapp/app/Plugin/Usermgmt/View/Elements/paginator.ctp
Pagination Element yourapp/app/Plugin/Usermgmt/View/Elements/pagination.ctp