Jump to content



Photo

Adding Dropdownlist To Autocomplete Field

sql filter ajax

  • Please log in to reply
5 replies to this topic

#1 Hermann

Hermann

    Member

  • Members
  • PipPip
  • 30 posts

Posted 11 December 2012 - 5:38 PM

Hi everybody. I have used the following code from http://www.yiiframew...-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(
[background=transparent]'[/background][background=transparent]minLength[/background][background=transparent]'[/background][background=transparent]=>[/background][background=transparent]'[/background][background=transparent]0[/background][background=transparent]'[/background][background=transparent],
[/background][background=transparent])[/background][background=transparent],
[/background][background=transparent]'[/background][background=transparent]htmlOptions[/background][background=transparent]'[/background][background=transparent]=>[/background][background=transparent]array[/background][background=transparent]([/background]
[background=transparent]'[/background][background=transparent]style[/background][background=transparent]'[/background][background=transparent]=>[/background][background=transparent]'[/background][background=transparent]height:20px;[/background][background=transparent]'[/background][background=transparent],
[/background][background=transparent])[/background][background=transparent],
[/background][background=transparent])[/background][background=transparent])[/background][background=transparent];[/background]



[background=transparent]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. [/background]

[background=transparent]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.[/background]


[background=transparent]Thank you very much for your help![/background]
  • 0

#2 Hermann

Hermann

    Member

  • Members
  • PipPip
  • 30 posts

Posted 11 December 2012 - 5:48 PM

I really don't know why this keeps happening. But here is the link to the code I used :http://www.yiiframew...-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.
  • 0

#3 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4,000 posts
  • LocationState College, PA (USA)

Posted 14 December 2012 - 6:57 PM

Sorry for the delay; I was out of town. Are you still having problems with this?
  • 0

#4 Hermann

Hermann

    Member

  • Members
  • PipPip
  • 30 posts

Posted 15 December 2012 - 11:55 AM

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!!
  • 0

#5 Hermann

Hermann

    Member

  • Members
  • PipPip
  • 30 posts

Posted 20 December 2012 - 3:22 AM

I have found this exact version of what I need but I'm not sure how to convert it to my own extension of Yii. Please have a look http://www.marcofoli...suggestion.html


Could anybody please help me?
  • 0

#6 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4,000 posts
  • LocationState College, PA (USA)

Posted 2 January 2013 - 9:38 AM

You kind of have a lot going on here. So you're trying to implement what that page explains but in a Yii-based site? And you want to do this as an extension of Yii, not just coded into the site from scratch?
  • 0