Jump to content
Larry Ullman's Book Forums

Adding Dropdownlist To Autocomplete Field


Hermann
 Share

Recommended Posts

Hi everybody. I have used the following code from http://www.yiiframework.com/wiki/361/simple-way-to-use-autocomplete-using-different-id-and-display-value/ . My table name is dog and I'm interested in three attributes: id, name and size. So far I have gotten an autocomplete field working which is populated with the name-attribute values as typed in by the end user. The following code does that:

 

Under extensions:

 

<?php

Yii::import("zii.widgets.jui.CJuiAutoComplete");

class myAutoComplete extends CJuiAutoComplete

{

 

/**

* Run this widget.

* This method registers necessary javascript and renders the needed HTML code.

*/

public function run()

{

list($name,$id)=$this->resolveNameID();

 

// Get ID Attribute of actual hidden field containing selected value

$attr_id = get_class($this->model).'_'.$this->attribute;

 

if(isset($this->htmlOptions['id']))

$id=$this->htmlOptions['id'];

else

$this->htmlOptions['id']=$id;

 

if(isset($this->htmlOptions['name']))

$name=$this->htmlOptions['name'];

 

if($this->hasModel()) {

echo CHtml::textField($name,$this->value,$this->htmlOptions);

echo CHtml::activeHiddenField($this->model, $this->attribute);

}else {

echo CHtml::textField($name,$this->value,$this->htmlOptions);

CHtml::hiddenField($name,$this->value,$this->htmlOptions);

}

 

if($this->sourceUrl!==null)

$this->options['source']=CHtml::normalizeUrl($this->sourceUrl);

else

$this->options['source']=$this->source;

 

// Modify Focus Event to show label in text field instead of value

if (!isset($this->options['focus'])) {

$this->options['focus'] = 'js:function(event, ui) {

$("#'.$id.'").val(ui.item.label);

return false;

}';

}

 

if (!isset($this->options['select'])) {

$this->options['select'] = 'js:function(event, ui) {

$("#'.$id.'").val(ui.item.label);

$("#'.$attr_id.'").val(ui.item.id);

}';

}

 

$options=CJavascript::encode($this->options);

//$options = $this->options;

 

$js = "jQuery('#{$id}').autocomplete($options);";

 

$cs = Yii::app()->getClientScript();

$cs->registerScript(__CLASS__.'#'.$id, $js);

}

}

 

 

 

Under models:

 

 

public static function usersAutoComplete($name='') {

 

 

$sql= 'SELECT id ,name AS label FROM dog WHERE name LIKE :name';

$name = $name.'%';

return Yii::app()->db->createCommand($sql)->queryAll(true,array(':name'=>$name));

 

 

}

 

Under Controllers:

public function actionUsersAutocomplete() {

$term = trim($_GET['term']) ;

 

if($term !='') {

 

$users = Users::usersAutoComplete($term);

echo CJSON::encode($users);

Yii::app()->end();

}

}

 

 

 

And finally under views:

 

$this->widget('ext.myAutoComplete', array(

'model'=>$model,

'attribute'=>'user_id',

'name'=>'user_autocomplete',

'source'=>$this->createUrl('dog/usersAutoComplete'),

'options'=>array(

'minLength'=>'0',

),

'htmlOptions'=>array(

'style'=>'height:20px;',

),

));

 

 

 

What I Want to achieve still is have a dropdown list with sizes (small, medium or large) which would be added to my SQL statement. I.e. if the dropdownlist is set to large then something like "....WHERE size='large'...." should be included in the SQL query. Im really not sure how to achieve this.

 

If no records are found then I need to display a "Sorry, no results" message in the autocomplete list and finally if a record is found and selected by the user a button needs to be included to do a database search based upon the 'id' found in the above code.

 

 

Thank you very much for your help!

Link to comment
Share on other sites

I really don't know why this keeps happening. But here is the link to the code I used :http://www.yiiframework.com/wiki/361/simple-way-to-use-autocomplete-using-different-id-and-display-value/

 

 

 

 

I would like to achieve the following but I'm not sure how:

 

1)have a dropdown list with sizes (small, medium or large) which would be added to my SQL statement. I.e. if the dropdownlist is set to large then something like "....WHERE size='large'...." should be included in the SQL query.

 

2)If no records are found then I need to display a "Sorry, no results" message in the autocomplete list

 

3)If a record is found and selected by the user a button needs to be included to do a database search based upon the 'id' found in the above code.

Link to comment
Share on other sites

Hi there Larry. Yes...and for the life of me I can't seem to figure this one out on my own. Previous problems became easy after a while but not this one. Could you please give me some advice?? BTW thank you for all the wonderful work in light of the Yii framework. This is the first time that I'm using a framework and decided on Yii as I learned PHP from one of your books and decided if you like Yii then it must be worth my while and thus far it most certainly is!!

Link to comment
Share on other sites

  • 2 weeks later...
 Share

×
×
  • Create New...