info@ektanjali.com +91 99276-99286

CakePHP 5.x User Management Premium Plugin Version 5.0.x

Ajax Search

 How to use normal search or ajax search in plugin and outside plugin

Features

  • Ajax Search is optional we can perform normal search
  • Search by field values with like, startswith, endswith, =, <, >, <=, >= etc conditions
  • Ajax suggestions on search inputs
  • search inputs types are text, select box, date picker input, checkbox
  • Search clear button

1. Include Search component in controller's initialize function for e.g.

$this->loadComponent('Usermgmt.Search');

2. set search fields variable in your controller for e.g. in UsersController.php

public $searchFields = [
    'index'=>[
        'Usermgmt.Users'=>[
            'Users'=>[
                'type'=>'text',
                'label'=>'Search',
                'tagline'=>'Search by name, username, email',
                'condition'=>'multiple',
                'searchFields'=>['Users.first_name', 'Users.last_name', 'Users.username', 'Users.email'],
                'searchFunc'=>['plugin'=>'Usermgmt', 'controller'=>'Autocomplete', 'function'=>'userIndexSearch'],
                'inputOptions'=>['style'=>'width:200px;']
            ]
        ]
    ]
];

options

For all possible option see Search Component getDefaultSearchOptions function

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

$cond = [];
$cond = $this->Search->applySearch($cond);

$this->paginate = ['limit'=>10, 'order'=>['Users.id'=>'DESC']];
$paginated = $this->paginate($this->Users->find()->where($cond));
$users = $paginated->toArray();

$this->set(compact('paginated', 'users'));

if($this->getRequest()->is('ajax')) {
    $this->viewBuilder()->setLayout('ajax');
    $this->render('/Users/all_users');
}

Please Note: $this->Search->applySearch(); is required to add before fetching result from database. If you add conditions in your query then same conditions should be passed in this applySearch function. for e.g.

$cond = [];
$cond['UserEmailSignatures.user_id'] = $this->UserAuth->getUserId();
$cond = $this->Search->applySearch($cond);

$this->paginate = ['limit'=>10, 'order'=>['UserEmailSignatures.id'=>'DESC']];
$paginated = $this->paginate($this->UserEmailSignatures->find()->where($cond));
$userEmailSignatures = $paginated->toArray();

for more information check yourapp/plugins/Usermgmt/src/Controller/UserEmailSignaturesController.php

4. Now in template 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/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 find the element file all_users.php in yourapp/plugins/Usermgmt/templates/Users

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

Now in element file you should add search form using Search Helper in following way before the results table

<?php echo $this->Search->searchForm('Users', ['legend'=>false, 'updateDivId'=>'updateUsersIndex']); ?>

where
'Search' is a helper location is yourapp/plugins/Usermgmt/src/View/Helper/SearchHelper.php.
'legend' is false/string, you can pass any string to make legend text
'updateDivId' is reference id to tell the Jquery code that which section will be updated on ajax call.

 

For more information see
Users Controller yourapp/plugins/Usermgmt/src/Controller/UsersController.php
Search Component yourapp/plugins/Usermgmt/src/Controller/Component/SearchComponent.php
Search Behavior yourapp/plugins/Usermgmt/src/Model/Behavior/SearchingBehavior.php
Index View yourapp/plugins/Usermgmt/templates/Users/index.php
Index View yourapp/plugins/Usermgmt/templates/Users/all_users.php
Search Helper yourapp/plugins/Usermgmt/src/View/Helper/SearchHelper.php
Search Form Element yourapp/plugins/Usermgmt/templates/element/search_form.php