Ok first things first. I was very hesitant to create another thread on the topic of Dependent Dropdowns but I have been through the wiki and a multitude of threads on creating a dependent dropdown.
In my current scenario I am working with a form and trying to use a dependent dropdown that grabs the values from two other different models.
I am to the point where I am not getting anymore problems displaying in Firebug so hopefully someone can put me on the right track because things are not working.
With the code below both of the dropdowns are properly "displaying" but there is no functionality (i.e. when I select a Category, nothing happens with the SubCategory)
db structure:
OrgCatChosen.(OrgCatChosenId, SubCategoryId, OrganizationId)
Category.(CategoryId, Name)
SubCategory.(SubCategoryId, CategoryId, Name)
Current code:
OrgCatChosenController:
public function actionDynamicSubCategories()
{
$data=Category::model()->findAll('CategoryId=:CategoryId',
array(':CategoryId'=>(int) $_POST['OrgCatChosen']['CategoryId']));
public function actionDynamicSubCategories()
{
$data=Category::model()->findAll('CategoryId=:CategoryId',
array(':CategoryId'=>(int) $_POST['OrgCatChosen']['CategoryId']));
$data=CHtml::listData($data,'CategoryId','Name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
}
public function accessRules() {
return array(
...
array('allow',
'actions'=>array('minicreate', 'create', 'update', 'admin', 'delete', 'DynamicSubCategories'),
'users'=>array('admin'),
),
OrgCatChosen/_form:
<div class="row">
<label>Category</label>
<?php
echo GxHtml::dropDownList('CategoryId','', GxHtml::listDataEx(Category::model()->findAll(),'CategoryId', 'Name'),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('OrgCatChosen/DynamicSubCategories'),
'update'=>'#'.CHtml::activeId($Subcategory,'SubcategoryId')
)
));
?>
</div><!-- row -->
<div class="row">
<?php echo $form->labelEx($model,'SubCategoryId'); ?>
<?php echo GxHtml::dropDownList('subCategory','',
GxHtml::listData(SubCategory::model()->findAll(),'SubCategoryId','Name'));
?>
</div><!-- row -->