Using array with CJuiAutoComplete

Hi

I’m using the CJuiAutoComplete in my site. As an example I have this code in my controller:




        public function actionNames()

        {

            $res=array(array('id' => 1, 'name' => 'Sam and Max'), array('id'=>2, 'name'=>"Full Throttle"));

            echo CJSON::encode($res);

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

        }



And in my view I’ve got:




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

                            'name'=>'game',

                            'sourceUrl'=>array('/game/names'),

                            'options'=>array(

                                'minLength'=>'2',

                            ),

                            'htmlOptions'=>array(

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

                        ),

                    )); ?>



As you can see, the actionNames returns a array of arrays. The reason for this is I want to use ‘name’ to fill the auto complete list, and then ‘id’ to use with actionView($id) when I submit the search form. How can I accomplish this?

Write text in field (for example ‘sam’) -> actionNames returns array(‘id’ => 1, ‘name’ => ‘Sam and Max’) -> (from this point i need help) autocomplete field is filled with var[‘name’] -> form submit redirects to page/view/var[‘id’]

You should build the array a little different, like in this post. (note the keys: ‘label’, ‘value’, ‘id’)

/Tommy

Okay, but how can I access the ‘id’-key when submitting the form?

And also, do I have to build the form (action and method) myself, or is that included in CJuiAutoComplete?

If you want to have the controller supply the ‘sourceUrl’ (returned) content to CJuiAutoComplete you’ll need to create the controller action.

You can find a couple of other examples of CJuiAutoComplete usage in Yii Playground.

Also search the forum for more hints.

/Tommy