CAutoComplete alternate lookup value

Hi folks,

I am using the CAutoComplete widget to lookup people in a "users" table based on a LIKE match on the users' names. However, once a person has been selected from the users table with the CAutoComplete box, I want to submit the form and have the database ID of the user posted back to the server, not just the person's name. (The name is only displayed for usability…the ID is what is really used by the code to find the user record and return associated data.) Is there a good way to do this? Any ideas are much appreciated. Thanks!

Please check the documentation at http://plugins.jquer…t/autocompletex

Take a look at those formatXXX functions.

Thanks for pointing me in the right direction, Qiang. For those interested, here's what I did to make this work:

  1. Since I am calling an action to return my dataset, I use Yii to query values for autocomplete population. In order to "return" multiple values, you have to split up your values using the pipe "|" delimiter. Here's sample code:


public function actionAutoPopulateLookup()


{


	if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))


	{


		$name				= $_GET['q'];


		$limit				= $_GET['limit'];


		$criteria				= new CDbCriteria;


		$criteria->condition	= "name LIKE :sterm";


		$criteria->params 	= array(":sterm"=>"%$name%");


		$criteria->limit		= $limit;


		$returnVal			= '';


		$userArray			= User::model()->findAll($criteria);


		foreach($userArray as $userAccount)


		{


			$returnVal .= $userAccount->getAttribute('name').'|'.$userAccount->getAttribute('user_id')."n";


		}


		echo $returnVal;


	}


	die();


}


  1. Once your php is returning pipe-delimited strings terminated by "\n", you can now use the associated values with your autocomplete widget and a hidden field, like so:


<?php echo $this->widget('CAutoComplete',


		array(


			'name'=>'user_name',


			'url'=>array('controller/action'),


				'max'=>10,


				'minChars'=>2,


				'delay'=>500,


				'matchCase'=>false,


				'htmlOptions'=>array('size'=>'40'),


				'methodChain'=>".result(function(event,item){$("#user_id").val(item[1]);})",


			));


?>


<?php echo CHtml::hiddenField('user_id'); ?>


Notice the methodChain attribute being used. This appends (or chains) a javascript method to the end of the js AutoComplete widget. This particular method is the "result" method, which references the hidden field and assigns the 2nd part (part after the pipe) of the selected autocomplete data to that field.

Hope that helps someone!

This is very nice. Could you please share it as a cookbook page? I think this will help other people a lot.

A new cookbook page has been created at http://www.yiiframew…oc/cookbook/25/. Feel free to provide suggestions for improvement.

I am currently working on a widget to have this but also some more  functionality.

-Like displaying various info fields of the selected entry

-customize the string in the drop down list (list of elements to display and design)

-customized search patterns via the widget options etc.

I also modified some code to return only json data to CAutocomple instead of html code.

I will post this extension once I finished my work.

But nice work ;)

Nice job! Thank you.

I have now uploaded my extension see here:

http://www.yiiframew…66.msg8320.html