info@ektanjali.com +91 99276-99286

CakePHP 2.x User Management Premium Plugin Version 2.3.x

Ajax Sorting

Sorting is very easy in Cake Php please make sure sorting works with CakePHP pagination. I have done some changes for normal sorting to ajax sorting.

How to use Ajax Sorting 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 View file you should use table headers in following way-

<th><?php echo $this->Paginator->sort('User.first_name'__('Name')); ?></th>
Now this table column for Name becomes ajax sort-able.

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