Creating Lookup field

I’m trying to make a lookupfield-like in my application.

The intention is that the user click on a browse-button, and it pops-up a dialog(widget) with a grid(CGridView) inside. The user could select a row, and the ‘Description’ column is sent to a textField into my form.

I’ve already done this part by registering the following script in the form:


Yii::app()->clientScript->registerScript('scriptName', '

function onSelectionChange()

{

        var keys = $("#CGridViewUsuario > div.keys > span");


        $("#CGridViewUsuario > table > tbody > tr").each(function(i)

        {

                if($(this).hasClass("selected"))

                {

                        $("#Funcionario_UsuarioId").val($(this).children(":nth-child(1)").text());

                }

        });

}

');

And my widget:




            <?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(

                    'id'=>'mydialog',


                    'options'=>array(

                        'title'=>'Usuário',

                        'width' => 'auto',

                        'autoOpen'=>false,

                    ),

                ));


            $this->widget('zii.widgets.grid.CGridView', array(

                'dataProvider' => Usuario::model()->searchByLogin($model->UsuarioId),

                'id' => 'CGridViewUsuario',

                'filter' => Usuario::model(),

                'columns' => array(

                    'Login',

                    'Nome',

                ),

                'htmlOptions' => array(

                    'style'=>'cursor: pointer;'

                    ),                   

                'selectionChanged'=>'js:function(id){ onSelectionChange(); }',

            ));


            $this->endWidget('zii.widgets.jui.CJuiDialog');

        ?>

Now there are two tasks for me to do:

  • When the user clicks the browse button, the CGridView should appear with the filter already filled with the input he typed in the form.

  • Put the CGridView filters to work.

Not forgetting that, If all this runs successfully, when the user clicks on the save button, I’ll have to save the corresponding ID of the lookupField in the model.