[SOLVED]cjuiAutocomplete help

i have this part in my _form file , am pulling 216,912 rows from the table that I think

it’s the cause why the page turns to white when used with a dropDownList.

can you give me tips how to implement the autocomplete thing if am gonna pull

the data using a model ? Tnx in advance




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

	'id'=>'wsmembersdetails-form',

	'enableAjaxValidation'=>false,

)); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'City'); ?>

		<?php #echo $form->dropDownList($model,'City',CHtml::listData(Worldareascities::model()->findAll(),'CityID','CityName')); ?>

		<?php #echo $form->textField($model,'City'); ?>

		<?php echo $form->error($model,'City'); ?>

	</div>





	<div class="row buttons">

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

	</div>


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



Controller:




    public function actionSuggestCity() {

        $criteria = new CDbCriteria;

        $criteria->select = array('id_city', 'city', 'province');

        $criteria->addSearchCondition('city', $_GET['term']);

        $criteria->limit = 15;

        $data = City::model()->findAll($criteria);


        $arr = array();

        foreach ($data as $item) {

            $arr[] = array(

                'id' => $item->id_city,

                'value' => $item->city,

                'label' => $item->city . ' (' . $item->province . ')',

            );

        }


        echo CJSON::encode($arr);

    }



View




<?php

        //to store the city_id from the coty after selecting a result from the autoComplete

        echo $form->hiddenField($model, 'city_id');


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

            'name' => 'Daytrip[city]',

            'sourceUrl' => array('city/suggestcity'),

            'value' => ($model->city->city) ? $model->city->city : $model->city,

            'options' => array(

                'showAnim' => 'fold',

                //remove if you dont need to store the id, like i do...

                'select' => 'js:function(event, ui){ jQuery("#Daytrip_city_id").val(ui.item["id"]); }'

            ),

            'htmlOptions' => array(

            //'style' => 'height:20px;'

            ),

        ));

?>



I have a problem, in my example the value is not submitted

In your example use id_city in your controller but in your model you use city_id is there any reason? maybe a don’t understand your example any help ?

My view




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

	'id'=>'personas-buscapersona-form',

	'enableAjaxValidation'=>false,

));


       

        echo $form->hiddenField($model, 'idperson');

     ?>

  <div class="row">

       <?

       

       echo $form->error($model,'idperson');


               

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

           'name' => 'Daytrip[nombre]',

            'sourceUrl' => array('reservatandem/SuggestPerson'),

            'value' =>  ($model->nombre) ? $model->nombre: $model->nombre,

            'options' => array(

                'showAnim' => 'fold',

                //remove if you dont need to store the id, like i do...

                'select' => 'js:function(event, ui){ jQuery("#Daytrip_nombre_id").val(ui.item["id"]); }'

            ),

            'htmlOptions' => array(

                'style'=>'height: 16px; width: 300px;',

             ),

        ));



controller




    public function actionSuggestPerson() {

        $criteria = new CDbCriteria;

        $criteria->select = array('idperson', 'nombre');


        $criteria->addSearchCondition('nombre',  strtoupper( $_GET['term']) ) ;

        $criteria->limit = 15;

        $data = Personas::model()->findAll($criteria);


        $arr = array();

        

        foreach ($data as $item) {

           

            $arr[] = array(

                'id' => $item->idperson,

                'value' => $item->nombre,

                'label' => $item->nombre,

            );

        }


        echo CJSON::encode($arr);

        

      }



change this

‘select’ => ‘js:function(event, ui){ jQuery("#Daytrip_nombre_id").val(ui.item[“id”]); }’

to this

‘select’ => ‘js:function(event, ui){ jQuery("#Daytrip_idperson").val(ui.item[“id”]); }’

this peacse of javascript will grab the id element from the json array and put it in the hidden textfield ($form->hiddenField($model, ‘idperson’);) with id Daytrip_idperson.

remark on your last answer. please put code between [code ] and [/code ]

@sergiorosales

When posting code in the forum use the <> button of the editor… or type [code ]…[/code ] (without spaces)…






NOTE: you can edit your post and change that, so your code is more readable...

Thanks, but that doesn’t work,

My View




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

	'id'=>'personas-buscapersona-form',

	'enableAjaxValidation'=>false,

));


       

       

     ?>

  <div class="row">

       <?

       

      // echo $form->error($model,'idperson');


        

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

           'name' => 'Daytrip[nombre]',

            'sourceUrl' => array('reservatandem/SuggestPerson'),

            'value' =>  ($model->nombre) ? $model->nombre: $model->nombre,

            'options' => array(

                'showAnim' => 'fold',

                //remove if you dont need to store the id, like i do...

              //  'select' => 'js:function(event, ui){ jQuery("#Daytrip_idperson").val(ui.item["id"]); }'

                'select' => 'js:function(event, ui){ jQuery("#Daytrip_idperson").val(ui.item["id"]); }'

            ),

            'htmlOptions' => array(

                'style'=>'height: 16px; width: 300px;',

             ),

        ));


         echo $form->hiddenField($model, 'idperson');

         




My Controller




public function actionSuggestPerson() {

        $criteria = new CDbCriteria;

        $criteria->select = array('idperson', 'nombre');


        $criteria->addSearchCondition('nombre',  strtoupper( $_GET['term']) ) ;

        $criteria->limit = 15;

        $data = Personas::model()->findAll($criteria);


        $arr = array();

        

        foreach ($data as $item) {

           

            $arr[] = array(

                'id' => $item->idperson,

                'value' => $item->nombre,

                'label' => $item->nombre,

            );

        }


        echo CJSON::encode($arr);

        

      }



Are you sure that


 echo $form->hiddenField($model, 'idperson');

creates the id: #Daytrip_idperson (used on the select line)

If your model is not Daytrip… then the ID is different… it’s better to use activeid() instead…

so your "select" line would be




...

'select' => 'js:function(event, ui){ jQuery("'.CHtml::activeId($model,'idperson').'").val(ui.item["id"]); }'

...



Oh thanks, now I undertood, you were right, a really apreciate your help.

The problem was in the model name

My error




'select' => 'js:function(event, ui){ jQuery("#Daytrip_idperson").val(ui.item["id"]); }'



changed by




'select' => 'js:function(event, ui){ jQuery("#Personas_idperson").val(ui.item["id"]); }'



Thanks mdomba and bas_vdl

Glad you solved it…

As I wrote above it’s better not to use fixed model names because it could happen that you change the class name in the future or reuse that code on another form or that Yii changes the generation of the ID’s (less probability)…

That’s why it’s better to use CHtml::activeId($model,‘idperson’) to get the ID for the model attribute…

Hi

i have a problem similar, but not find the solution

my code

in the view





<?php

echo $form->hiddenField($model, 'idProfesional');

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

	'name'=>'Medico[descripcion]',

	'value'=>($model->profesional) ? $model->profesional->persona->nro_documento.' '.$model->profesional->persona->apellido. ' '. $model->profesional->persona->nombre:'',

	'sourceUrl'=>array('medico/MedicosAjax'),

    'htmlOptions'=>array(

                  'size'=>'60'

          ),

	'options'=>array(

			'showAnim'=>'fold',

                    //'select' => 'js:function(event, ui){ jQuery("'.CHtml::activeId($model,'idProfesional').'").val(ui.item["id"]); }'

                    'select' => 'js:function(event, ui){ jQuery("#Medico_idProfesional").val(ui.item["id"]); }'


	),

)); ?>



in the controller





public function actionMedicosAjax() {

    

        $criteria = new CDbCriteria;

        $criteria->select = array('idProfesional', 'nro_documento', 'apellido','nombre');

        $criteria->addSearchCondition('apellido', $_GET['term']);

        $criteria->limit = 15;

        $criteria->with = 'persona';

        

        $data = Rp_profesionales::model()->findAll($criteria);


        $arr = array();

        foreach ($data as $item) {

            $arr[] = array(

                'id' => $item->idProfesional,

                'value' => $item->nro_documento,

                'label' => $item->apellido . ' (' . $item->nombre . ')',

            );

        }

 

        echo CJSON::encode($arr);

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


    }




html generate




<input name="Medico[idProfesional]" id="Medico_idProfesional" type="hidden" value="54178" />


<input size="60" id="Medico_descripcion" type="text" value="27746605 HENDERSON VANESA" name="Medico[descripcion]" />


<script type="text/javascript">/*<![CDATA[*/jQuery(function($) {jQuery('#Medico_descripcion').autocomplete({'showAnim':'fold','select':function(event, ui){ jQuery("#Medico_idProfesional").val(ui.item["id"]); },'source':'/sistemas/index.php?r=medico/MedicosAjax'});$('#medico-form').yiiactiveform({'attributes':[{'inputID':'Medico_obs','errorID':'Medico_obs_em_','model':'Medico','name':'obs','status':1}],'summaryID':'medico-form_es_'});});/*]]>*/</script>




help!!! and thanks

Do you get any error? What is the problem?

solved

bad code




$criteria->select = array('idProfesional', 'nro_documento', 'apellido','nombre');

$criteria->addSearchCondition('apellido', $_GET['term']);



solution

remove select




//$criteria->select = array('idProfesional', 'nro_documento', 'apellido','nombre');



and add the alias at field apellido




$criteria->addSearchCondition('Personas.apellido', $_GET['term']);

...




        foreach ($data as $item) {

            $arr[] = array(

                'id' => $item->idProfesional,

                'value' => $item->persona->nro_documento,

                'label' => $item->persona->apellido . ' (' . $item->persona->nombre . ')',

            );

        }




thanks

Thankx, this is a great topic.

Great post! thank you, however, when using this example, hidden field and ac textbox get the same id (’#Model_attribute’)

Changing manually the id for ac textbox will prevent value state saving between callbacks

What can I do to solve this issue?

I am using a protected function in my Controller File to create the widget (and calling it from the view file):




protected function _createCityPicker($model, $attr, $name) {

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

            array(

                'sourceUrl' => array('ride/clist'),

                'name' => $name,

                'options' => array(

                    'showAnim' => 'fold',

                    'minLength' => '2',

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

                        $('#" . CHtml::activeId($model, $attr) . "').val(ui.item['id']);

                    }",

                ),

            ));

    }



This is the code in the View File




echo $form->label($model, 'cityfrom_id');

    $this->_createCityPicker($model, 'cityform_id', 'Ride[cityfrom_id]');


    echo $form->hiddenField($model, 'cityfrom_id');



BTW I am using a different model with ac