I am new in Yii framework and I cannot solve this problem with dropdown.
In my page I have two dropdown lists, both of them multiselect dropdowns. And second one depends first dropdown. For example,
1-dropdown is fruits, vegetables, drinks;
2-dropdown depends user selection, if user select fruit and drinks, it should be display apple, grapes, juice, bier ( all together in one dropdownlist );
If user select only one option e.g vegetables in first dropdown, second should display onion, potato etc.
Here source code:
//---------- VIEW ------------
<div id="left">
<div class="btn-group">
<?php
echo CHtml::dropDownList('group', '', CHtml::listData($group,'group','group'), array(
'multiple'=>'multiple',
'ajax'=>array(
'type' => 'POST',
'url' => CController::createUrl('site/change'),
'update' => '#e5'
),
'id' => 'e4',
'style' => 'width:300px'
));
?>
</div>
</div>
<div id="right">
<div class="btn-group">
<?php
echo CHtml::dropDownList('subgroup','', array(), array(
'multiple' => 'multiple',
'id'=>'e5',
'style' => 'width:300px'
));
?>
</div>
</div>
// ------------ CONTROLLER ------------
public function actionChange()
{
$groupName = $_POST['group'];
$dpCriteria = new CDbCriteria();
$dpCriteria->select = 'subgroups';
$dpCriteria->condition = 'group = '.$groupName;
$data = Subgroups::model()->findAll($dpCriteria);
$data = CHtml::listData($data,'subgroups','subgroups');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
The method you are asking is Dependent Dropdown
. YII framework have tutorials for creating Dependent Dropdown
. Check this link for more details and examples
EDIT:
In your code, you are checking groupname with single value as 'group = '.$groupName;
. But $groupname
is an array of value. So you have use in
in mysql with the values of imploded $groupname array.
Your condition in criteria is:
$dpCriteria->condition = 'group in ('.implode(",",$groupName).')';
Try like this.In your ajax array,
'update'=>'#'.CHtml::activeId($model,'attribute_to_update'),
'data'=>array('atribute'=>'js:this.value'),