Yii Framework Forum: CJuiAutoComplete an Idiots Guide? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CJuiAutoComplete an Idiots Guide? Autocomplete using Model/Database Rate Topic: -----

#1 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 09 March 2012 - 06:13 AM

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.
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 11 March 2012 - 05:42 PM

View PostThePaulius, on 09 March 2012 - 06:13 AM, said:

<? 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,
 ));
?>



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:

  • 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();

0

#3 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 14 March 2012 - 11:20 AM

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.
0

#4 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 15 March 2012 - 05:02 AM

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...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
0

#5 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 22 March 2012 - 06:04 AM

So have you found any solution?
0

#6 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 22 March 2012 - 11:41 AM

View Postbennouna, on 22 March 2012 - 06:04 AM, said:

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:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users