Cjuiautocomplete Not Working For Me

I have gone through a number of tutorials and have modified this code several times but can’t figure out why it is not working. Pls someone should help me out.

I have a method in my controller class (/protected/controller/BrandController.php)which is:


public function actionBrandSearch($term)

	{

		//$term = $_GET['term'];

		

		 $query ="SELECT name FROM fbv_brand WHERE name LIKE :string";


        $command =Yii::app()->db->createCommand($query);


        $command->bindValue(":string", '%'.$term.'%', PDO::PARAM_STR);


        $result =$command->queryAll();


        echo CJSON::encode($result);


        Yii::app()->end();

	}

Then I have in (/protectedd/view/brand/_form.php)


<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(

	'action'=>Yii::app()->createUrl('brand'),

	'method'=>'get',

)); ?>    

    

<div class="row">

    <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(

	'name'=>'name',

	'sourceUrl'=>$this->createUrl('brand/brandSearch'),// <- path to controller which returns dynamic data

	// additional javascript options for the autocomplete plugin

	'options'=>array(

		'showAnim'=>'fold',

		'minLength'=>'1', // min chars to start search

		'select'=>'js:function(event, ui) { alert(ui.item.id +":"+ui.item.value); }'

	),

));

?>

    </div>

    

	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>

<?php $this->endWidget(); ?>

Please what is missing, what am I failing to do? I access this form with r=brand/create, the form shows but no response from db.

Hi Israel Ama,

As you see in the API documentation of JUI Autocomplete, the source array must be in one of the following 2 formats:

http://api.jqueryui.com/autocomplete/#option-source

So you have to change the format of array that you’ve got with queryAll.

Try this:




public function actionBrandSearch($term)

{

	$query ="SELECT name FROM fbv_brand WHERE name LIKE :string";

	$command =Yii::app()->db->createCommand($query);

	$command->bindValue(":string", '%'.$term.'%', PDO::PARAM_STR);

	$result =$command->queryAll();


	$ret = array_values($result);


	/* or ...

	$ret = array();

	foreach($result as $record) {

		$ret[] = array(

			'label' => $record['name'],

			'value' => $record['name'],

		);

	}

	*/


	echo CJSON::encode($ret);

	Yii::app()->end();

}



And, yeah, welcome to the forum!

Hi Softark,

Thanks a lot for the prompt response. It’s my pleasure to be here!

I’m restructuring to make it work.I’d give feedback. Thanks again.

Hi Softark and All,

I have implemented the above suggestion but did NOT get any results.

My questions:

(i) Is it compulsory to attach a model to the CJuiautocomplete, as someone said in one tutorial. I have done that but no results obtained. If it is compulsory, maybe I did not do it well.

(ii) In calling the


'sourceUrl'=>$this->createUrl('brand/brandSearch'),

I am correct since the method name is actionBrandSearch()? I followed the pattern I saw in some tutorials.

Sorry, I was wrong in the previous post.

The code should be:




public function actionBrandSearch($term)

{

	$query ="SELECT name FROM fbv_brand WHERE name LIKE :string";

	$command =Yii::app()->db->createCommand($query);

	$command->bindValue(":string", '%'.$term.'%', PDO::PARAM_STR);

	$result =$command->queryAll();


	$ret = array();

	foreach($result as $record) {

		$ret[] = $record['name'];

	}


	/* or ...

	$ret = array();

	foreach($result as $record) {

		$ret[] = array(

			'label' => $record['name'],

			'value' => $record['name'],

		);

	}

	*/


	echo CJSON::encode($ret);

	Yii::app()->end();

}



It is not compulsory, but using ‘model’ and ‘attribute’ pair instead of ‘name’ and ‘value’ pair will make things easier when you want to use the widget in CActiveForm.

I think it’s fine.

see demo

http://www.eha.ee/labs/yiiplay/index.php/en/site/widget?view=autocomplete

and download the complete code from

install it on your localhost and enjoy!

Thanks for this info.

It worked!