I am sorry could not give you full example code 'cause i have no time now. Still doing project. But have source link then keep read and practice is a better way than
@daonhack: we have had this link before, but i am realy new to this (working with CJuiAutocomplete).
@tri: i tried this example before, but the id doesn’t get posted, and if i change the value in this array to the id i need for post, it is shown in autocomplete-field after selection. i’d like to get the value shown in the autocomplete-field and the ID set in an hiddenfield or else on selecting an entry in the autocomplete-field.
@mdomba: yes, this is what i am searching for too )
and if i get the list by following code, how do i get the ID in the widget to set it into a hidden-fields value?
$arr = array();
foreach($models as $model) {
$arr[] = array(
'label'=>$model->some_attr, // label for dropdown list
'value'=>$model->some_attr, // value for input field
'id'=>$model->id, // return value from autocomplete
);
}
echo CJSON::encode($arr);
in JuiController.php i did following in actionAutocompleteTest():
$arr = array();
foreach($models as $model) {
$arr[] = array(
'label'=>$model->some_attr, // label for dropdown list
'value'=>$model->some_attr, // value for input field
'id'=>$model->id, // return value from autocomplete
);
}
echo CJSON::encode($arr);
If is possible to use a custom seletc without it, it is not possible (at least, I couldn’t) to stylize properly the items, like in this example.
The only way I found is to implement the method chain in CJuiAutoComplete, it is quite simple as this class has less than 40 lines of code, I just copied all and added 2 small changes:
Here my solution:
<?php
Yii::import('zii.widgets.jui.CJuiInputWidget');
class FJuiAutoComplete extends CJuiInputWidget
{
public $source = array();
public $sourceUrl;
/* first change */
public $methodChain = null;
public function run()
{
list($name,$id)=$this->resolveNameID();
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::activeTextField($this->model,$this->attribute,$this->htmlOptions);
else
echo CHtml::textField($name,$this->value,$this->htmlOptions);
if($this->sourceUrl!==null)
$this->options['source']=CHtml::normalizeUrl($this->sourceUrl);
else
$this->options['source']=$this->source;
$options=CJavaScript::encode($this->options);
/*second change*/
$js = "jQuery('#{$id}').autocomplete($options){$this->methodChain};";
$cs = Yii::app()->getClientScript();
$cs->registerScript(__CLASS__.'#'.$id, $js);
}
}
To developers: do you consider this solution valid, and will you include in future release of the framework? It really gives more possiblity of usage for the JuiAutoComplete
The idea is that if the user changes the value, I delete the the id. For example, if there is a user called ‘John Smith’ with an Id = 24, the widget will return the id if ‘John Smith’ is selected from the list, but if is changed to ‘John Smithson’, then the Id will come blank.