Yii Framework Forum: Activeform Autocomplete - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Activeform Autocomplete Rate Topic: -----

#1 User is offline   Darka 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-May 13

Posted 15 May 2013 - 12:34 PM

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();
		}
	}

1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users