Search code examples
autocompleteyiiwidgetyii-extensions

Yii CJuiAutoComplete values are not getting displayed


I apologise if this is already exists but i could not get it to work.. I need to show the names and store the id's in my table. these name are coming from other model(table). i'm getting the names and id's when i inspect elts in chrome network inspector.. but values are not not visible..but they are populating when i try to click on them its coming up in alert with id..

can anyone pls help me to get names visible --thanks for ur help

my _form.php is something like this:

     <div class="row">
        <?php echo $form->labelEx($typeModel,'benefit_type'); ?>
        <?php
    $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'attribute'=>'name',
    'model'=>$typeModel,
    'sourceUrl'=>array('benefit/benefit_type_list'),
    'value'=>'Please select',
    'name'=>'name',
    'id'=>'id',
    'options'=>array(
        'minLength'=>'0',
         'select'=>"js:function(event, ui) {
     alert(ui.item.id);
                                        // $('#organisation_id').val(ui.item.id);
                                        }",
    ),
    'htmlOptions'=>array(
'id'=>'id',
    'size'=>45,
    'maxlength'=>45,
    ),
    )); ?>

    <?php echo $form->error($typeModel,'benefit_type'); ?>

extension class for getting the names and id's is:

   <? class EAutoCompleteAction extends CAction
    {
        public $model;
        public $attribute;
        public $id;
        private $results = array();
        public $returnVal = '';
        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};
                    //$this->results[] = $m-<{$this->id};
                    $this->results[] = array(
                        'name' => $m->{$this->attribute},
                            'id'=> $m->id
                    );



                    /* $this->returnVal .= $m->getAttribute('name').'|'
                    .$m->getAttribute('id'). "\n"; */
                }

            }

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

and in my controller:

public function actions()
    {
        return array(
                'benefit_type_list'=>array(
                        'class'=>'application.extensions.EAutoCompleteAction',
                        'model'=>'BenefitType', //My model's class name
                        'attribute'=>'name', //The attribute of the model i will search
                ),
        );

    }

Solution

  • It's been a while since I used this extension but it uses Jquery's auto complete, and the dataset from EAutoCompleteAction probably needs:

                    $this->results[] = array(
                        'label' => $m->{$this->attribute},
                        'value'=> $m->id,
                        'id'=> $m->id
                    );
    

    Source: yii forums