Cjuiautocomplete

CJuiAutoComplete + multimodelform:

It works fine for the first row, but the second does not show autocomplete support

What’s wrong?

this is my _form.php


<?php

/* @var $this PedidoController */

/* @var $model Pedido */

/* @var $form CActiveForm */

?>


<div class="form wide">


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

	'id'=>'pedido-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Los campos marcados con <span class="required">*</span> son obligatorios.</p>


	<?php echo $form->errorSummary(array_merge(array($model),$validatedDetalle)); ?>


	<div class="row">

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

		<?php echo $form->textField($model,'numeroPedido',array('disabled' => true,'style'=>'width:80px;color:green;')); ?> 

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

	</div>


	<div class="row">

		<?php	

			$detalleFormConfig = array(

				'elements'=>array(

				

				'descripcion'=>array(

					'type'=>'zii.widgets.jui.CJuiAutoComplete',

						'name'=>'descripcion',

						'value'=>$detalle->descripcion,

						'visible' => true,

						'source'=>$this->createUrl('autocompletedescripcion'),

                		'options'=>array(

							'showAnim'=>'fold',

						),

			    ),

				

				'precio'=>array(

					'type'=>'text',

					'size'=>'10',

					'maxlength'=>10,

					'id'=>'precio',

					'STYLE'=>'text-align:right; width:60px',

				),

			));

			

		

			$this->widget('ext.multimodelform.MultiModelForm',array(

				'id' => 'id_detalle', //the unique widget id

				'formConfig' => $detalleFormConfig, //the form configuration array

				'model' => $detalle, //instance of the form model

				'validatedItems' => $validatedDetalle,

				'data' => empty($validatedItems) ? $detalle->findAll('pedido_id=:idPedido', array(':idPedido'=>$model->id)): null,

				'showAddItemOnError' => false,

				'tableView' => true, //sortable will not work

				'fieldsetWrapper' => array('tag' => 'div', 'htmlOptions' => array('class' => 'view','style'=>'position:relative;background:#EFEFEF;')),

				'removeLinkWrapper' => array('tag' => 'div', 'htmlOptions' => array('style'=>'position:absolute; top:1em; right:1em;')),

				'addItemText' => 'Agregar',

				'removeText' => 'Eliminar', 

				'removeConfirm' => '¿ Eliminar el artículo seleccionado ?', 

				 

			));

		?>

	</div>

	

 	<div class="row buttons">

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

	</div>

	

	

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

</div><!-- form -->




My MultiModelForm.php …


/**

     * Support for CJuiAutoComplete. 

     *

     * @contributor Smirnov Ilya php1602agregator[at]gmail.com

     * @param array $element

     * @return string

     */

    public static function afterNewIdAutoComplete($element)

    {

        $options = isset($element['options']) ? $element['options'] : array();

        if (isset($element['sourceUrl']))

            $options['source'] = CHtml::normalizeUrl($element['sourceUrl']);

        else

            $options['source'] = $element['source'];

 

        $jsOptions = CJavaScript::encode($options);

 

        return "if ( this.hasClass('ui-autocomplete-input') )

            {

                var mmfAutoCompleteParent = this.parent();

                // cloning autocomplete element (without data and events)

                var mmfAutoCompleteClone  = this.clone();

 

                // removing old autocomplete element

                mmfAutoCompleteParent.empty();

                // re-init autocomplete with default options

                mmfAutoCompleteClone.autocomplete({$jsOptions});

 

                // inserting new autocomplete

                mmfAutoCompleteParent.append(mmfAutoCompleteClone);

            }";

    }

Thanks so much !!!

I think you have to use different names for autocomplete names…

Hi!

I’ve been trying to make it work, but i cant.

Did you make it work?

Hola!

Yo no lo he logrado que me resulté.

Lograste que te funcionara??

Hi,

Just try to give the different name and assign different id in htmloption.I hope it will solve your problems

Thanks

chandran nepolean

Hi!

The ids are differents. But i don’t know how to give them differents names, can you give me an example.

Thanks!!

OK, now it work. But only for the last element. Apperently, that’s how it’s done, working only for the last element.

Another little problem, it also removes the label from the field with the autocomplete.

public static function afterNewIdAutoComplete($element)

{


	&#036;options = isset(&#036;element['options']) ? &#036;element['options'] : array();


	if (isset(&#036;element['sourceUrl']))


		&#036;options['source'] = CHtml::normalizeUrl(&#036;element['sourceUrl']);


	else


		&#036;options['source'] = &#036;element['source'];





	&#036;jsOptions = CJavaScript::encode(&#036;options);





	return &quot;if ( this.hasClass('ui-autocomplete-input') )


		{


			var mmfAutoCompleteParent = this.parent();


			// cloning autocomplete element (without data and events)


			var mmfAutoCompleteClone  = this.clone();





			// removing old autocomplete element


			mmfAutoCompleteParent.empty();[color=&quot;#FF0000&quot;] //THIS IS WHERE REMOVES THE AUTOCOMPLETE FROM THE PREVIUOS[/color]


			// re-init autocomplete with default options


			mmfAutoCompleteClone.autocomplete({&#036;jsOptions});





			// inserting new autocomplete


			idTemp = mmfAutoCompleteClone.attr('id');


			mmfAutoCompleteClone.attr('name', idTemp);


			mmfAutoCompleteParent.append(mmfAutoCompleteClone);


		}&quot;;


}

Hi,

just use rand() php method with name attribute, it will make different names :)

Thanks

chandran nepolean

Hi Fart,

It simple coding.

My code works fine.

In memberFormConfig of view :




'no_kk'=>array(

                'type'=>'zii.widgets.jui.CJuiAutoComplete',

                #'maxlength'=>20,

				'source'=>$this->createUrl('Kk/autoKk'),  

                'options'=>array(  

					'showAnim'=>'fold',  

                    'minLength' => '1',

					'maxLenghh' => '5',

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

						$("#no_kk").val(ui.item.id);  

                        return false;  

                        }'  

				),

				'htmlOptions'=>array(

				'size'=>'10',

				#'id'=>'no_kk',

				),

            ),



in widget multimodelform of view




$this->widget('ext.multimodelform.MultiModelForm',array(

            'id' => 'id_member', //the unique widget id

            'formConfig' => $memberFormConfig, //the form configuration array

            'model' => $member, //instance of the form model

			'tableView'=>true,

			'jsAfterNewId' => MultiModelForm::afterNewIdDatePicker($memberFormConfig['elements']['tgl_aktif']),

			'jsAfterNewId' => MultiModelForm::afterNewIdAutoComplete($memberFormConfig['elements']['no_kk']),

 

            //if submitted not empty from the controller,

            //the form will be rendered with validation errors

            'validatedItems' => $validatedMembers,

 

            //array of member instances loaded from db

            'data' => $member->findAll('no_rumah=:no_rumah', array(':no_rumah'=>$model->no_rumah)),

        ));



In controller :




public function actionautoKk()  

    {  

        $res =array();  

        $row=array();  

        if (isset($_GET['term'])){  

			$sql ="SELECT * FROM kk WHERE no_kk LIKE :cityname";  

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

            $command->bindValue(":cityname", ''.$_GET['term'].'%', PDO::PARAM_STR);  

            $res =$command->queryAll();  

            foreach($res as $value):  

				$row[]=array(  

                'label'=>$value['no_kk'].' '.$value['kepala_keluarga'], // label for dropdown list       

                'value'=>$value['no_kk'], // value for input field       

                'id'=>$value['no_kk'],

				#'nama'=>$value['NamaPeserta'],// return value from autocomplete  

                );   

                endforeach;  

           }  

           echo CJSON::encode($row);  

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

    }



-Sigit-