Difference between #2 and #3 of
Using CAutoComplete to display one value and submit another

Revision #3 has been created by wei on Mar 25, 2009, 11:09:16 PM with the memo:

fix line wrapping in code.
« previous (#2) next (#4) »

Changes

Title unchanged

Using CAutoComplete to display one value and submit another

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

[...]
//...
public function actionAutoCompleteLookup()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
       $name                = $_GET['q']; // q is the default var name that is used by
 
// 
the autocomplete widget to pass in user input $limitname  = $_GET['limit']; q']; 
 
// this was set with the "max" attribute of the CAutoComplete widget           $limit               = $_GET['limit']; 
 
$criteria = new CDbCriteria;
$criteria->condition = "name LIKE :sterm";
$criteria->params = array(":sterm"=>"%$name%");
[...]
foreach($userArray as $userAccount)
{
$returnVal .= $userAccount->getAttribute('name').'|'

 
                 
.$userAccount->getAttribute('user_id')."\n";
}
echo $returnVal;
[...]
<?php echo $this->widget('CAutoComplete',
array(
             'name'=>'user_name', //name of the html field that will be generated 'url'=>array('controller/action'),name'=>'user_name', 
 
//replace controller/action with real ids              'url'=>array('controller/action'), 
 
'max'=>10, //specifies the max number of items to display              'minChars'=>2,  
 
//specifies the number of chars that must be entered
 
 //
before autocomplete initiates a lookup              'minChars'=>2, 
 
'delay'=>500, //number of milliseconds before lookup occurs 'matchCase'=>false, //match case when performing a lookup?              'htmlOptions'=>array('size'=>'40'),  
 
//any additional html attributes that go inside of
 
 //
the input field can be defined here              'htmlOptions'=>array('size'=>'40'), 
 
 
'methodChain'=>".result(function(event,item){\$(\"#user_id\").val(item[1]);})",
));
?>
<?php echo CHtml::hiddenField('user_id'); ?>
```
Take note of the `methodChain` attribute being used. MethodChain essentially appends (or chains) a javascript method to the end of the AutoComplete javascript code that will be generated. This particular method is the `result` method, which fires when an autocomplete item is selected. The code inside of the result function basically references the hidden field that was defined and assigns the 2nd part (part after the pipe) of the selected autocomplete data to that field.
[...]
7 3
11 followers
Viewed: 65 517 times
Version: 1.1
Category: Tutorials
Written by: luoshiben
Last updated by: mohammad sharif ahrari
Created on: Mar 25, 2009
Last updated: 12 years ago
Update Article

Revisions

View all history