info@ektanjali.com +91 99276-99286

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

Ajax Search

I have developed a search element for filtering the results in this plugin. This search option works with or with out Ajax.
Auto suggestions on search fields works with this search form.

How to use Search Options for filtering the results in Cake Php
1. Include Search helper in controller's Helper variable for e.g. public $helpers = array('Usermgmt.Search');

2. Include Search component in controller's Component variable for e.g. public $components = array('Usermgmt.Search');

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

var $searchFields = array
        (
            'index' => array(
                'User' => array(
                    'User.id'=> array(
                        'type' => 'text',
                        'condition' => '=',
                        'label' => 'User Id',
                        'inputOptions'=>array('style'=>'width:50px;')
                    ),
                    'User.first_name'=> array(
                        'type' => 'text',
                        'label' => 'Name'
                    ),
                    'User.username' => array(
                        'type' => 'text',
                        'label' => 'Username'
                    ),
                    'User.email' => array(
                        'type' => 'text',
                        'label' => 'Email'
                    ),
                    'User.user_group_id' => array(
                        'type' => 'select',
                        'condition' => 'comma',
                        'label' => 'Group',
                        'model' => 'UserGroup',
                        'selector' => 'getGroups'
                    ),
                    'User.email_verified' => array(
                        'type' => 'select',
                        'label' => 'Email Verified',
                        'options' => array(''=>'Select', '0'=>'No', '1'=>'Yes')
                    ),
                    'User.active' => array(
                        'type' => 'select',
                        'label' => 'Status',
                        'options' => array(''=>'Select', '1'=>'Active', '0'=>'Inactive')
                    )
                )
            )
        );

where
'index' is a function on which you want to set Search FIlter.
'User' is a model name for which you are going to display results.
'User.id' is a field name of User model for displaying a option to search on id. here you can use other model fox ex belongsTo, HasOne etc. For more information see var $searchFields in UsersController.php, look code for online function here I used other model 'UserActivity.status'
'type' is search field type for e.g. 'text', 'select', 'checkbox'.
'label' is search form field label.
'condition' is search field condition which will use in database query for e.g. '=', 'like', 'startswith', 'endswith', 'comma'. default is 'like'.
'selector (used only with 'type' => 'select')' is function name in given model for pop up the select box values.
'model' (used only with 'type' => 'select') is model name in which selector function is defined.
'options' (used only with 'type' => 'select') is options array for pop up the select box values. This can be used in the place of 'selector' and 'model'.
'inputOptions' additional information for input field.


4. Include Jquery and auto complete in View file

<?php echo $this->Html->script(array('/usermgmt/js/jquery-1.7.2.min''/usermgmt/js/um.autocomplete')); ?>

5. 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.

Now find the div with id "updateUserIndex" got it?? The section inside this div will update on every ajax call.

I have added element call in this div section

<?php echo $this->element('Usermgmt.all_users'); ?>

6. Now in View file you should add search form using Search Helper in following way for e.g. in all_users.ctp before the results table

<?php echo $this->Search->searchForm('User', array('legend' => 'Search'"updateDivId" => "updateUserIndex")); ?>

where
Search is a helper for creating search form, location is yourapp/app/Plugin/Usermgmt/View/Helper/SearchHelper.php.
'User' is a model name. The same you pass in form creation.
'updateDivId' is id of main div. The contents of this div will update on every ajax call. I have explained this in step 5 above. In index.ctp I have set this id "updateUserIndex" you can change this according your wish.

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.ctp
Index View yourapp/app/Plugin/Usermgmt/View/Elements/all_users.ctp
Search Helper yourapp/app/Plugin/Usermgmt/View/Helper/SearchHelper.ctp
Search Form Element yourapp/app/Plugin/Usermgmt/View/Elements/search_form.ctp