Hello
really want to make a combobox autocomplete, but I found there was
CookBook 25
but the page shows nothing
my code…
controler
<?php
class UsuarioRolesController extends CController
.....
public function actionAutoCompleteLookup()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
/* q is the default GET variable name that is used by
/ the autocomplete widget to pass in user input
*/
$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;
$rolArray = Roles::model()->findAll($criteria);
$returnVal = '';
foreach($rolArray as $rol)
{
$returnVal .= $rol->getAttribute('nombre').'|'
.$rol->getAttribute('id_rol')."\n";
}
echo $returnVal;
}
die();
}
......
view
<div class="simple">
<?php $this->widget('CAutoComplete',
array(
//name of the html field that will be generated
'name'=>'rol_nombre',
//replace controller/action with real ids
'url'=>array('usuarioRoles/AutoCompleteLookup'),
'max'=>10, //specifies the max number of items to display
//specifies the number of chars that must be entered
//before autocomplete initiates a lookup
'minChars'=>2,
'delay'=>500, //number of milliseconds before lookup occurs
'matchCase'=>false, //match case when performing a lookup?
//any additional html attributes that go inside of
//the input field can be defined here
'htmlOptions'=>array('size'=>'40'),
'methodChain'=>".result(function(event,item){\$(\"#id_rol_hidden\").val(item[1]);})",
));
?>
<?php echo CHtml::hiddenField('id_rol_hidden'); ?>
</div>
model
<?php
class UsuarioRoles extends CActiveRecord
{
/**
* The followings are the available columns in table 'UsuarioRoles':
* @var integer $id_usuario_rol
* @var integer $usu_id_usuario
* @var integer $rol_id_rol
*/
/**
* Returns the static model of the specified AR class.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'UsuarioRoles';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
//array('rol_id_rol, usu_id_usuario', 'unique'),
array('rol_id_rol, usu_id_usuario', 'required'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'usuario' => array(self::BELONGS_TO, 'Usuarios', 'usu_id_usuario'),
'rol' => array(self::BELONGS_TO, 'Roles', 'rol_id_rol'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id_usuario_rol' => 'id Usuario Rol',
'usu_id_usuario' => 'Usuario',
'rol_id_rol' => 'Rol',
);
}
}
thanks !!!