darka
(Bakunas)
1
Could not find in forums how to add autocomplete text field to TbActiveForm (which is bootstrap widget for CActiveForm), so here it is:
In form view, register jquery ui js and css first:
cs()->registerCoreScript('jquery.ui');
cs()->registerCssFile(Yii::app()->clientScript->getCoreScriptUrl().'/jui/css/base/jquery-ui.css');
cs()->registerScript('autocomplete','
jQuery("#' . CHtml::activeId($model, "username") . '").autocomplete({"showAnim":"fold","source":"/user/ajaxGetUserList"});
')
Form field is added as usual:
<?php echo $form->textFieldRow($model, 'username', array('class'=>'span2','maxlength'=>255));?>
And in controller:
public function actionAjaxGetUserList(){
if ( Yii::app()->request->isAjaxRequest ) {
$data = array();
if ( isset( $_GET['term'] ) ) {
$qtxt ="SELECT username FROM " . User::model()->tableSchema->name . " WHERE username LIKE :match LIMIT 10";
$command =Yii::app()->db->createCommand( $qtxt );
$command->bindValue( ":match", '%'.$_GET['term'].'%', PDO::PARAM_STR );
$data =$command->queryColumn();
}
echo CJSON::encode( $data );
Yii::app()->end();
}
}
http://yiibooster.clevertech.biz/jquery.html
for autocomplete
http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete
darka
(Bakunas)
3
Right you can use autocomplete widget, but then form model validation errors won’t show up.