CakePHP 4.x User Management Premium Plugin Version 4.1.x
How to use ajax pagination in plugin and outside plugin
1. Set pagination variable in your controller e.g. in UsersController.phppublic $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->viewBuilder()->setLayout('ajax');
$this->render('/Users/all_users');
}
3. Now in template file code is conditional code for reducing the duplicate code. i.e. template 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/templates/Users/index.php
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('../Users/all_users'); ?>
4. Now open the all_users file, location is yourapp/plugins/Usermgmt/templates/Users/all_users.php
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 before the results table<?php echo $this->element('Usermgmt.paginator', ['useAjax'=>true, 'updateDivId'=>'updateUsersIndex']); ?>
where
'Usermgmt.paginator' is a paginator element location is yourapp/plugins/Usermgmt/templates/element/paginator.php.
'useAjax' is true/false, set true for ajax pagination and set false for normal pagination.
'updateDivId' is reference id to tell the Jquery code that which section will be updated on ajax call.
Now in this file you should add pagination element in following way e.g. in all_users.php after the results table<?php
if(!empty($users)) {
echo $this->element('Usermgmt.pagination', ['paginationText'=>__('Number of Users')]);
}?>
where
$users is result variable, set from controller's action.
'Usermgmt.pagination' is a pagination element location is yourapp/plugins/Usermgmt/templates/element/pagination.php.
'paginationText' is a pagination text to be displayed on pagination bar.
For more information see
Users Controller yourapp/plugins/Usermgmt/src/Controller/UsersController.php
Index View yourapp/plugins/Usermgmt/templates/Users/index.php
Index View yourapp/plugins/Usermgmt/templates/Users/all_users.php
Paginator Element yourapp/plugins/Usermgmt/templates/element/paginator.php
Pagination Element yourapp/plugins/Usermgmt/templates/element/pagination.php