CJuiAutoComplete an Idiots Guide?

Ok, i’ve been turning myself inside out for a couple of days, and ive tried all the tutorials, copied others code, and I just cant do it.

Im looking for a guide that will tell me exactly how to make a field use the autocomplete facility which grabs all the distinct values in a DB column, and allow that all to work within an create and update form…

e.g

Put <? xyz ?> in controllers/LeadsController.php

Put <? xyz ?> in leads/_form.php

Install xyz into extensions/…

I can at least get it to show the appropriate value from a column, I cant get it to create a dynamic list to choose from.

e.g. in my _form ive got currently (and have had millions of attempts, I dont know what worked and what didnt now :-X




<? echo $form->textField($model,'client_address_town',array()); ?>

<?

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

    'model'=>$model,

    'attribute'=>'client_address_town',

    'source'=>$this->createUrl('leads/update'),

    // additional javascript options for the autocomplete plugin

    'options'=>array(

        'showAnim'=>'fold',

        'select'=>"js:function(event, ui) {

            $('#Leads_client_address_town').val(ui.item.id);

        }"

    ),

    'cssFile'=>false,

 ));

?>



Where’s Obi Wan Kenobi when you need him.

For my needs, I used jQuery Select To Autocomplete on top of JUI Autocomplete, which makes is straightforward since it simply hides your (select) dropdown list so you change nothing in your view, instead of adding the widget.

Now for your code, it seems you use client_address_town twice in your view. I may be wrong but you end up with two fields whose HTML id is Leads_client_address_town … am I wrong? If that’s the case, I think it would mess with the JS. Otherwise, sorry not to help.

Unless… if you’re stuck with JUI Autocomplete, here’s my code:

[list=1]

[*]Make sure you download add jquery.select-to-autocomplete.js

And also refer to jquery-ui-autocomplete.js (since you won’t use a widget, Yii won’t know you need it)

I use


Yii::app()->clientScript->registerScriptFile($baseUrl.'/js/jquery-ui-autocomplete.js', CClientScript::POS_END);

Yii::app()->clientScript->registerScriptFile($baseUrl.'/js/jquery.select-to-autocomplete.js', CClientScript::POS_END);



[*]You’re ready: in your view, use (well I made up the model and attribute names):


<?php echo $form->dropDownList($model,

				'client_address_town',

				CHtml::listData(Town::model()->findAll(), 'id', 'townName')

			); ?>

[*]Somewhere, add your javascript:


$('#Leads_client_address_town').selectToAutocomplete();

[/list]

Hey tellibus,

Thanks for that, i will go play with your stuff…

I though that the Yii framework had an autocomplete built into it… ?! all i need is for a input field to dynamically offer suggestions based on other data also inputed for that filed in the past… to make it quicker each time…

Thanks again, im off to try it now.

btw, before I bail on the native stuff, this is what im trying with now, and failing!

I get the following in my browser console

"GET http://***.domain.co.uk/index.php?r=leads/update&term=ong 400 (CHttpException)"

And I get this no matter what method I use to auto complete… Im getting confused. trying to search for the word ‘Ongar’

…extensions/EAutoCompleteAction.php ( Code from here )


<?php

class EAutoCompleteAction extends CAction

{

    public $model;

    public $attribute;

    private $results = array();

 

    public function run()

    {

        if(isset($this->model) && isset($this->attribute)) {

            $criteria = new CDbCriteria();

            $criteria->compare($this->attribute, $_GET['term'], true);

            $model = new $this->model;

            foreach($model->findAll($criteria) as $m)

            {

                $this->results[] = $m->{$this->attribute};

            }

 

        }

        echo CJSON::encode($this->results);

    }

}

?>

LeadsController.php


public function actions()

	{

		// return external action classes, e.g.:

	    return array(

        'update'=>array(

        'class'=>'application.extensions.EAutoCompleteAction',

        'model'=>'Leads', //My model's class name

        'attribute'=>'Leads_client_address_town', //The attribute of the model i will search

      ),

    );

	}



_form.php


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

      'attribute'=>'client_address_town',

        'model'=>$model,

        'sourceUrl'=>array('leads/update'),

        'name'=>'client_address_town',

        'options'=>array(

          'minLength'=>'3',

        ),

        'htmlOptions'=>array(

          'size'=>45,

          'maxlength'=>45,

        ),

  ));

ARGH! is basically where im at right now lol

So have you found any solution?

No, i’ve had to move on to the next problem, and hope when I revist this issues, i’ll have learnt Yii. I just cant afford to spend too long on any one problem…

:unsure: