info@ektanjali.com +91 99276-99286

CakePHP 2.x User Management Premium Plugin Version 2.4.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 if your controller is outside plugin directory for e.g.

public $components = ['Usermgmt.Search'];

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

public $searchFields = [
    'index'=>[
        'Usermgmt.User'=>[
            'User'=>[
                'type'=>'text',
                'label'=>'Search',
                'tagline'=>'Search by name, username, email',
                'condition'=>'multiple',
                'searchFields'=>['User.first_name', 'User.last_name', 'User.username', 'User.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, 'conditions'=>$cond, 'order'=>['User.id'=>'DESC']];
$users = $this->paginate('User');

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

if($this->request->is('ajax')) {
    $this->layout = '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['UserEmailSignature.user_id'] = $this->UserAuth->getUserId();
$cond = $this->Search->applySearch($cond);

$this->paginate = ['limit'=>10, 'conditions'=>$cond, 'order'=>['UserEmailSignature.id'=>'DESC']];
$userEmailSignatures = $this->paginate('UserEmailSignature');

for more information check yourapp/app/Plugin/Usermgmt/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/app/Plugin/Usermgmt/View/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/app/Plugin/Usermgmt/View/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/app/Plugin/Usermgmt/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/app/Plugin/Usermgmt/Controller/UsersController.php
Search Component yourapp/app/Plugin/Usermgmt/Controller/Component/SearchComponent.php
Search Behavior yourapp/app/Plugin/Usermgmt/Model/Behavior/SearchingBehavior.php
Index View yourapp/app/Plugin/Usermgmt/View/Users/index.php
Index View yourapp/app/Plugin/Usermgmt/View/Users/all_users.php
Search Helper yourapp/app/Plugin/Usermgmt/View/Helper/SearchHelper.php
Search Form Element yourapp/app/Plugin/Usermgmt/View/Elements/search_form.php