info@ektanjali.com +91 99276-99286

CakePHP 3.x User Management Premium Plugin Version 3.3.x

Ajax Sorting

 How to use ajax sorting in plugin and outside plugin

1. Set pagination variable in your controller e.g. in UsersController.php

public $paginate = ['limit'=>25];

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

$this->paginate = ['limit'=>10, 'order'=>['Users.id'=>'DESC']];
$users = $this->paginate($this->Users)->toArray();
$this->set(compact('users'));
if($this->getRequest()->is('ajax')) {
    $this->layout = 'ajax';
    $this->render('/Element/all_users');
}

3. Now in template ctp file code is conditional code for reducing the duplicate code. i.e. View code is divided in two parts one for ajax updation and other for normal request.

Just open the index file, location is yourapp/plugins/Usermgmt/src/Template/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 yourapp/plugins/Usermgmt/src/Template/Element/

Now find the div with id "updateUsersIndex" The section inside this div will update on every ajax call.

Now 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', ['ajax'=>true, 'updateDivId'=>'updateUsersIndex']); ?>

where
'Usermgmt.paginator' is a paginator element location is yourapp/plugins/Usermgmt/src/Template/Element/paginator.ctp.
'ajax' is true/false, set true for ajax sorting and set false for normal sorting
'updateDivId' is reference id to tell the Jquery code that which section will be updated on ajax call.

Now in Template file you should use table headers in following way

<th class="psorting"><?php echo $this->Paginator->sort('Users.first_name', __('Name')); ?></th> 
Now this table column for Name becomes ajax sort-able.

where
'psorting' is a class which is required for ajax sorting

For more information see
Users Controller yourapp/plugins/Usermgmt/src/Controller/UsersController.php
Index View yourapp/plugins/Usermgmt/src/Template/Users/index.ctp
Index View yourapp/plugins/Usermgmt/src/Template/Element/all_users.ctp
Paginator Element yourapp/plugins/Usermgmt/src/Template/Element/paginator.ctp