Add additional value before ajax call when using Yii CAutoComplete

If you are like me who wish to add an additional value into Yii CAutoComplete and wondering how this can be done, it is pretty simple thanks to Ron Lavie. Assuming, you have the default ajax call on your controller which is “$_GET['q']“, now, you would like to get a new value to be included into your controller called “$_GET['f']” and you are wondering what to do in order to get your value into Yii CAutoComplete before it fires ajax call to your controller, this is how you should do it.

'extraParams'=>array('f'=>"js:function(){return $(\"#f\").val();}"),

Using the extraParams, we can add in additional field before an ajax call to the controller. Here’s a full illustration.

      $this->widget('CAutoComplete',
          array(
             'value'=> $text,
             'name'=>'nanny_name',
             'id' => 'nanny_name',
                         //replace controller/action with real ids
             'url'=>array('/task/admin/autoCompleteNannyLookup'),
             'max'=>10, //specifies the max number of items to display
                         //specifies the number of chars that must be entered
                         //before autocomplete initiates a lookup
             'minChars'=>1,
             'delay'=>500, //number of milliseconds before lookup occurs
             'matchCase'=>false, //match case when performing a lookup?
                         //any additional html attributes that go inside of
                         //the input field can be defined here
             'htmlOptions'=>array('size'=>'40'),
             'extraParams'=>array('f'=>"js:function(){return $(\"#f\").val();}"),                                                                                   
             'methodChain'=>".result(function(event,item){\$(\"#updateValue\").val(item[1]);})",
          )); 
    ?>  

Now, whenever something is filled up on this CAutoComplete, it will search for the value in id “f” and included into variable “f” before firing up the ajax call to your controller. icon smile Add additional value before ajax call when using Yii CAutoComplete

 

Like this post? Share it!

digg 48 Add additional value before ajax call when using Yii CAutoComplete reddit 48 Add additional value before ajax call when using Yii CAutoComplete stumbleupon 48 Add additional value before ajax call when using Yii CAutoComplete delicious 48 Add additional value before ajax call when using Yii CAutoComplete furl 48 Add additional value before ajax call when using Yii CAutoComplete technorati 48 Add additional value before ajax call when using Yii CAutoComplete google 48 Add additional value before ajax call when using Yii CAutoComplete myspace 48 Add additional value before ajax call when using Yii CAutoComplete facebook 48 Add additional value before ajax call when using Yii CAutoComplete twitter 48 Add additional value before ajax call when using Yii CAutoComplete
share save 171 16 Add additional value before ajax call when using Yii CAutoComplete

No related posts.

About Clay

I am Clay who is the main writer for this website. I own a small web hosting company in Malaysia and i'm available to be hired as individual contractor on elance or odesk. You can find me on twitter.
This entry was posted in How-to, Yii and tagged . Bookmark the permalink.

Comments are closed.