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

#1 User is offline   Darka 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • 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();
		}
	}

2

#2 User is offline   sefburhan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 147
  • Joined: 08-February 12
  • Location:islamabad

Posted 22 May 2013 - 12:49 AM

View PostDarka, on 15 May 2013 - 12:34 PM, said:

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.cl...biz/jquery.html

for autocomplete
http://www.yiiframew...JuiAutoComplete
1

#3 User is offline   Darka 

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

Posted 22 May 2013 - 11:21 AM

Right you can use autocomplete widget, but then form model validation errors won't show up.
0

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