There is very good example of CJuiAutocomplete with multiple JSON variable at www.eha.ee/labs/yiiplay/index.php/en/site/widget?view=autocomplete . There is no need to add ‘methodChain’. default CJuiAutocomplete has option to support multiple variables. Remember your JSON return should be in this format:
//this line is important.
<?php echo $form->hiddenField($model,'user_id'); ?>
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'username',
'source'=>$this->createUrl('jui/autocompleteTest'),
// additional javascript options for the autocomplete plugin
'options'=>array(
'showAnim'=>'fold',
'select'=>"js:function(event, ui) {
$('#user_id').val(ui.item.id);
}" //note #user_id
),
'cssFile'=>false,
));
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);
I tried with the previous example but i’m getting the values and it is coming up with id in alertbox as well…when i checked the network inspector in chrome…values are coming up fine. but the values are not being visible.
Hi, there is my solution I’ve made thank’s to your replies. I improved it to have a nice combobox with kind of “must match” function and scrollable results. I only show the view form part as the controller one does not change a lot from the examples above.
<div class="row">
<?php echo CHtml::label('User', 'uidlabel'); ?>
<style>
.ui-autocomplete {
max-height: 280px;
overflow-y: auto;
/* prevent horizontal scrollbar */
overflow-x: hidden;
}
/* IE 6 doesn't support max-height
* we use height instead, but this forces the menu to always be this tall
*/
* html .ui-autocomplete {
height: 280px;
}
#alertMatch
{
display:none;
}
</style>
<?php
$currentCompletename = $model->uid->cn;
$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
'name'=>'combobox',
'source' => $this->createUrl('/specialUser/autoCompleteLookup'),
'value' => $currentCompletename,
// additional javascript options for the autocomplete plugin
'options'=>array(
'minLength'=>'3',
'delay' => 500,
'select'=>'js:function(event, ui) {'
. '$("#SpecialUser_uid").val(ui.item.id);'
// Alert removal
. '$("#alertMatch").slideUp(500, function() {'
. '$("#alertMatch").html("")'
. '});'
. '}',
'change'=>'js:function(event, ui) {'
// Value does not match or field is empty
. 'if (!ui.item || $(this).val()=="") {'
// Alert only whith case value does not match
. 'if ($(this).val()!="") {'
. '$("#alertMatch").html("<div class=' . "'" . 'error' . "'" . '><b>Unknown User. Choose a matching value in the list</b></div>");'
. '$("#alertMatch").slideDown(250);'
. '};'
// In both cases sets an empty string in the hidden field
// (id SpecialUser_uid as SpecialUser is the name of the model and uid the attribute)
. '$("#SpecialUser_uid").val("");'
. '$(this).val("");'
. '}'
. '}',
'showAnim' => 'fold'
),
'htmlOptions'=>array(
'style' => 'width: 331px',
'id'=>'getNameIDs',
'rel'=>'val'
),
));
echo '<div id="alertMatch"></div>';
echo CHtml::activeHiddenField($model,'uid');
echo CHtml::error($model, 'uid'); ?>
</div>
Im using the plugin and it works fine! But I want to retrieve the ID of the selected item and I get the NAME. How can I get the id? The way I use to retrieve the field is Yii::app()->request->getPost(‘field_id_save’, ‘’);