MultimodelForm y Autocomplete (EJEMPLO)

Hola, espero que este le sirva a muchos.

Tengo en un MultiModelForm un Autocomplete que me trae los datos de producto, en el detalle de factura de compra, anteriormente sólo llenaba los campos de la primera fila. Con esta solución se obtiene el indice correspondiente a la fila y llena los datos de la misma.

CompraController.php




public function actionAutocomplete($term) 

        {

            $criteria = new CDbCriteria;

            $criteria->compare('LOWER(Descripcion)', strtolower($_GET['term']), true);

            $criteria->order = 'Descripcion';

            $criteria->limit = 30; 

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

  

            if (!empty($data))

            {

             $arr = array();

             foreach ($data as $item) {

              $arr[] = array(

                  'id' => $item->CodProducto,

                  'value' => $item->Descripcion,

                  'label' => $item->Descripcion,

                  'Precio' => $item->PreCompra,

                  'UniMedida'=>$item->UniMedida,

                  'Saldo'=>$item->CanExistencia,

                  

              );

             }

            }

            else

            {

             $arr = array();

             $arr[] = array(

              'id' => '',

              'value' => 'No se han encontrado resultados para su búsqueda',

              'label' => 'No se han encontrado resultados para su búsqueda',

             );

         }

  

         echo CJSON::encode($arr);

        }



_form.php




 $memberFormConfig = array(

          'elements'=>array(

            'CodProducto'=>array(

                'type'=>'text',

                 'size'=>'10',

                'maxlength'=>10,

                'style'=>'WIDTH:100px',

            ),

              

             'Descripcion'=>array(

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

                'source'=>$this->createUrl('compra/autocomplete'),

                'options'=>array(

                    'showAnim'=>'fold',

                    'common_id_string'=>'Descripcion',

                         'size'=>'120',

                         'minLength'=>'2', // Minimo de caracteres que hay que digitar antes de relizar la busqueda

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

                          var nomobj_texto = this.id; //El identificador del campo en mi caso #Detallecompra_Descripcion 

                          var indexid = nomobj_texto.substring(25,nomobj_texto.length); 

                          $('#Detallecompra_CodProducto'+indexid).val(ui.item.id); 

                          $('#Detallecompra_Precio'+indexid).val(ui.item.Precio); 

                          $('#Detallecompra_UniMedida'+indexid).val(ui.item.UniMedida);

                          $('#Detallecompra_Descuento'+indexid).val(0);

                          $('#Detallecompra_Exento'+indexid).val(0);

                         }",

                ),

                'htmlOptions'=> array(

                        'size'=>30,

                   'onFocus'=>"init(this.id)",

                        'placeholder'=>'Buscar ...',

                        'title'=>'Indique el producto.'

                ),

            ),

                    

            'Cantidad'=>array(

                'type'=>'text',

                //it is important to add an empty item because of new records

                'maxlength'=>8,

                'size'=>8,

               'style'=>'WIDTH:80px',

               'onchage'=>'calcularPrecioIVA()',

                

            ),

            'Precio'=>array(

                'type'=>'text',

                'maxlength'=>8,

                'size'=>8,

                'style'=>'WIDTH:80px',

            ),

            'UniMedida'=>array(

                'type'=>'text',

                'maxlength'=>8,

                'size'=>8,

                'style'=>'WIDTH:80px',

            ),

            'Descuento'=>array(

                'type'=>'text',

                'maxlength'=>8,

                'size'=>8,

                'style'=>'WIDTH:80px',

            ),

            'Exento'=>array(

                'type'=>'text',

                'maxlength'=>10,

                'size'=>8,

                'style'=>'WIDTH:80px',

            ),

            'SubTotal'=>array(

                'type'=>'text',

                'maxlength'=>10,

                'size'=>8,

                'style'=>'WIDTH:80px',

                'onchage'=>'calcularPrecioIVA()',

            ),

              'Saldo'=>array(

                'type'=>'text',

                'maxlength'=>10,

                'size'=>8,

                'style'=>'WIDTH:80px',

            ),

        ));

    

            $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,

            //if submitted not empty from the controller,

            //the form will be rendered with validation errors

            'validatedItems' => $validatedMembers,

            

//            'clearInputs'=>true,

//             'jsBeforeNewId' => "alert(this.attr('id'));",   

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

            //array of member instances loaded from db

//            'jsBeforeNewId' => "alert(this.attr('id'));",

//              'jsAfterCloneCallback'=>'alertIds',

                

                

//                'jsAfterNewId' => "alert(this.attr('id'));",


                

                

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

            'showAddItemOnError' => false,

            'addItemText' => 'Agregar',

            'removeText' => 'Eliminar',

            'removeConfirm' => '¿ Eliminar el producto seleccionado ?', 

        ));