en la vista …

 $this->widget('CAutoComplete',
			          array(
                                        'name'=>'nombre',
                                        'value'=>$nombre,
                                        'multiple'=>true,
                                        'url'=>array('ficha/autoCompletarAlgo'),
                                        'max'=>10, //specifies the max number of items to display
                                        'minChars'=>3,
                                        'delay'=>500, //number of milliseconds before lookup occurs
                                        'matchCase'=>false, //match case when performing a lookup?
                                        'mustMatch'=>true,
                                        'htmlOptions'=>array(
                                            'size'=>'60',
                                            //'onkeydown' => 'return deshabilitarEnter(event)'
                                            )
			          ));

en el controlador …
recordar darle permiso en accessRules

public function actionAutoCompletarAlgo()
        {
            if (Yii::app()->request->isAjaxRequest && isset($_GET['q']))
            {
                $name = $_GET['q'];
                // this was set with the "max" attribute of the CAutoComplete widget
                $limit = min($_GET['limit'], 50);
                $criteria = new CDbCriteria;
                $criteria->condition = "nombre LIKE :sterm";
                $criteria->params = array(":sterm" => "%$name%");
                $criteria->limit = $limit;
                $todos_los_nombres = modelo::model()->findAll($criteria);
                $arreglo_de_todos_los_nombres = array();
                foreach($todos_los_nombres as $todos)
                {
                    $cadena = str_replace("'", "\'", $todos['nombre']);
                    array_push($arreglo_de_todos_los_nombres, $todos['nombre']);
                }
                $lista_de_nombres =  implode ("\n", $arreglo_de_todos_los_nombres);
                echo $lista_de_nombres;
            }
        }

One Response to “Auto completar con Yii Framework”

  1. It’s a good thing you wrote this article instead of me because I couldn’t come up with all this original content like you did. You are simply an incredible writer.

Leave a Reply