Aadhira
(Kalaimaruthalingam)
April 27, 2013, 5:02am
1
hi all,
I am getting options for a dropdown from controller ,its working fine .I need those options to be seleced.I tried
echo CHtml::tag('option',array('value' => $id,'selected'=>'selected'),CHtml::encode($value),true);
But its not working ,can any one suggest me how to do this? ?
This is my code in controller’s action
foreach($data as $id => $value)
{
echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}
Thanks in advance
ajberri
(Alex)
April 27, 2013, 5:08am
2
If you’re trying to have a specific option selected, you need to make use of the selected html attribute. i.e:
foreach($data as $id => $value) {
if (condition)
echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
else
echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}
Aadhira
(Kalaimaruthalingam)
April 27, 2013, 5:12am
3
If you’re trying to have a specific option selected, you need to make use of the selected html attribute. i.e:
foreach($data as $id => $value) {
if (condition)
echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
else
echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}
I need all the options to be selected,and i already tried your code too …
ajberri
(Alex)
April 27, 2013, 5:29am
4
Oh, so it’s a multi select dropdown?
foreach($data as $id => $value) {
echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
}
Aadhira
(Kalaimaruthalingam)
April 27, 2013, 5:34am
5
Oh, so it’s a multi select dropdown?
foreach($data as $id => $value) {
echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
}
yes ,and one more thing i have to say ,i used Tbselect2 extension for the drop down in javascipt,is the problem due to this?
Aadhira
(Kalaimaruthalingam)
April 27, 2013, 5:40am
6
Aadhira:
yes ,and one more thing i have to say ,i used Tbselect2 extension for the drop down in javascipt,is the problem due to this?
this is my code in view
<?php echo form->listBox($model,'hobbies',CHtml::listData(hobbies::model()->findAllByAttributes(array('ID'=>$model->cat_id)),'hob_ID','hob_name'),
array( 'multiple'=>'multiple',
));
?>
and i used this in javascript
$('#hobbies').select2({
height:'150px'
});
ajberri
(Alex)
April 27, 2013, 5:41am
7
See this: http://yiitest.dev.bezz.com.au/
That’s just a simple select box with all items selected, with:
$data = array('john' => 'doe', 'alex' => 'bergman', 'josh' => 'smyth', 'sean' => 'norrish', 'adam' => 'kerr', 'sam' => 'gummer');
echo CHtml::openTag('select', array('multiple' => 'multiple', 'style' => 'width: 300px;'));
foreach($data as $id => $value) {
echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value));
}
echo CHtml::closeTag('select');
And you can see it works. So if you’re using select2 and it’s not working, there’s your problem. I’d suggest you read the select2 documentation to see if you can resolve the issue.
Aadhira
(Kalaimaruthalingam)
April 27, 2013, 5:45am
8
See this: http://yiitest.dev.bezz.com.au/
That’s just a simple select box with all items selected, with:
$data = array('john' => 'doe', 'alex' => 'bergman', 'josh' => 'smyth', 'sean' => 'norrish', 'adam' => 'kerr', 'sam' => 'gummer');
echo CHtml::openTag('select', array('multiple' => 'multiple', 'style' => 'width: 300px;'));
foreach($data as $id => $value) {
echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value));
}
echo CHtml::closeTag('select');
And you can see it works. So if you’re using select2 and it’s not working, there’s your problem. I’d suggest you read the select2 documentation to see if you can resolve the issue.
Thanks a lot ,i will try to fix the issue