I have two different form fields that are doing this (same form) the first part is a dependent dropdown and the second is a tree dropdown.
Here is all the code for the dependent dropdown. You select a manufacturer and it shows all the products for that manufacturer in the second.
model
//creates the dependent dropdown menu between company and products in _form.php 'product' => array(self::BELONGS_TO, 'Products', 'product_id'),
controller
public function actionProducts() { { $data=Products::model()->findAll('company_id=:company_id', array(':company_id'=>(int) $_POST['company_id'])); $data=CHtml::listData($data,'id','model'); foreach($data as $value=>$name) { echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true); } } }
Form
<div class="row"> <?php echo $form->labelEx($model, 'company_id'); ?> <?php $companies = CHtml::listData(Companies::model()->findAll(array('order' => 'id')), 'id', 'name'); echo CHtml::dropDownList('company_id','',$companies, array( 'prompt'=>'Select Manufacturer', 'ajax' => array( 'type'=>'POST', 'url'=>CController::createUrl('Products'), 'update'=>'#product_id', ) )); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'product_id'); ?> <?php echo CHtml::dropDownList('product_id','', array(), array( 'style' => 'width:170px;', 'prompt'=>'Select Product Model', )); ?> <?php echo $form->error($model, 'product_id'); ?> </div>
This is the second field that isn't saving it is a tree dropdown. It pulls that data from the locations table and displays them in a parent child tree view in the dropdown. i.e.
1
-1.1
--1.1.1
2
3
-3.1
--3.2
---3.3
model
'parent' => array(self::BELONGS_TO, 'Locations', 'id'), 'children' => array(self::HAS_MANY, 'Locations', 'parent'),
form
<div class="row"> <?php echo $form->labelEx($model,'location'); ?> <?php $parents = Locations::model()->findAll('parent = 0'); $cm = new CommonMethods(); $data = $cm->makeDropDown($parents); echo $form->dropDownList($model,'parent', $data, array('style'=>'width: 185px;', 'empty'=>'Please Select Location')); ?> <?php echo $form->error($model,'location'); ?> </div>
CommonMethods.php
<?php class CommonMethods { private $data = array(); public function makeDropDown($parents) { global $data; $data = array(); $data['0'] = ''; foreach($parents as $parent) { $data[$parent->id] = $parent->name; $this->subDropDown($parent->children); } return $data; } public function subDropDown($children,$space = '--') { global $data; foreach($children as $child) { $data[$child->id] = $space.$child->name; $this->subDropDown($child->children,$space.'--'); } } } ?>

Help with this would be greatly appreciated!