[{"key1":"value1", "key2":"value2"},{"key3":"value3","key4":"value4"}]
CJuiAutoComplete HowTo Display one value and submit another with CJuiAutoComplete
#21
Posted 17 July 2011 - 12:30 AM
#22
Posted 21 September 2012 - 12:53 AM
<?php echo $form->hiddenField($model,'user_id'); ?>
in _form.php i did following:
//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);
Wiwo inc.
| Mobile: 919995504508
#23
Posted 01 October 2012 - 03:43 AM
$this->widget(
'zii.widgets.jui.CJuiAutoComplete',
array(
'model' => $model,
'attribute' => '{attribute}',
'source' => array_keys(CHtml::listData($model->findAll(array('select' => '{attribute}')), '{attribute}', '{attribute}')),
'options' => array(
'minLength' => '2',
)
)
);
#24
Posted 11 October 2012 - 01:29 PM
I need to select a name, but submit an Id....
It's so difficult
Ricordalo quando fai il debug
#25
Posted 01 December 2012 - 11:27 PM
realtebo, on 11 October 2012 - 01:29 PM, said:
I need to select a name, but submit an Id....
It's so difficult
Answer is here. Look at last example and browse at code.
www.eha.ee/labs/yiiplay/index.php/en/site/widget?view=autocomplete
I had use this in my project and it work.
#26
Posted 02 January 2013 - 09:17 AM
rurbinac, on 14 June 2011 - 09:07 PM, said:
'options'=>array(
'showAnim'=>'fold',
'select'=>"js:function(event, ui) {
$('#User_user_id').val(ui.item.id);
}",
'change'=>"js:function(event, ui) {
if (!ui.item) {
$('#User_user_id').val('');
}
}",
),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.
I am still testing it but it has worked so far
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.
you can see my full code in here:
<div class="row">
<?php echo $form->labelEx($typeModel,'benefit_type'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'model'=>$model,
'attribute'=>'name',
'source'=>$this->createUrl('benefit/benefit_type_list'),
// additional javascript options for the autocomplete plugin
'options'=>array(
'showAnim'=>'fold',
'select'=>"js:function(event, ui) {
$('#User_user_id').val(ui.item.id);
}"
),
'cssFile'=>false,
));
?>
<?php echo $form->error($typeModel,'benefit_type'); ?>
</div>
I tried both the ways but output is same values are not being displayed..
<div class="row">
<?php echo $form->labelEx($typeModel,'benefit_type'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'attribute'=>'name',
'model'=>$typeModel,
'sourceUrl'=>array('benefit/benefit_type_list'),
'value'=>'Please select',
'name'=>'name',
'id'=>'id',
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'0',
'select'=>"js:function(event, ui) {
alert(ui.item.id);
// $('#BenefitType_benefit_type_id').val(ui.item.id);
}",
),
'cssFile'=>false,
'htmlOptions'=>array(
'id'=>'id',
'size'=>25,
'maxlength'=>25,
),
)); ?>
<?php echo $form->error($typeModel,'benefit_type'); ?>
</div>can anyone pls help me
Thanks in advance
#27
Posted 02 January 2013 - 12:19 PM
thanks in advance..
<div class="row">
<?php echo $form->hiddenField($typeModel,'id'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete',
array('model' => $typeModel,
'attribute' => 'name',
'source' => array_keys(CHtml::listData($typeModel->findAll(array('select' => 'name')), 'name', 'id')),
'options' => array(
'minLength' => '1',
)
));
?>
</div>Im trying something like this but its not coming up in controller
var_dump($_POST['TypeModel']['id']);die;
#28
Posted 02 January 2013 - 12:31 PM
Evgeniy Tkachenko, on 01 October 2012 - 03:43 AM, said:
$this->widget(
'zii.widgets.jui.CJuiAutoComplete',
array(
'model' => $model,
'attribute' => '{attribute}',
'source' => array_keys(CHtml::listData($model->findAll(array('select' => '{attribute}')), '{attribute}', '{attribute}')),
'options' => array(
'minLength' => '2',
)
)
);
Thanks alot for your simply superb cjuiautocomplete code
#29
Posted 02 March 2013 - 06:11 AM
<?php echo $form->labelEx($model,'name'); ?>
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'name',
// 'type'=>'POST',
'source'=>$this->createUrl('/RamDbf/getNameIDs'),// <- path to controller which returns dynamic data
// additional javascript options for the autocomplete plugin
'options'=>array(
'minLength'=>'1', // min chars to start search
'select'=>'js:function(event, ui) {
$("#DiagonisticEntryForm_name").val(ui.item.id);
}'
),
'htmlOptions'=>array(
'id'=>'getNameIDs',
'rel'=>'val',
'style' => 'width: 315px'
),
));
?>
<?php echo $form->error($model,'name'); ?>
controller:
public function actiongetNameIDs(){
$list = array();
$term = isset($_REQUEST['term']) ? trim($_REQUEST['term']) : "";
$term = addcslashes($term, '%_'); // escape LIKE's special characters
$criteria = new CDbCriteria();
$params = array();
if(!empty($term)){
$criteria->condition = "name LIKE :term" ;
$params[':term'] = "%$term%";
}
$criteria->params = $params;
$criteria->offset = 0;
$criteria->limit = 50;
$criteria->order = "id ASC";
$referers = RamDbf::model()->findAll($criteria);
foreach($referers as $test){
$item = array();
$item['id'] = $test->id;
$item['value'] = $test->name;
$list[] = $item;
}
echo json_encode($list);
Yii::app()->end();
}

Help













