Hello guys!
I’ve already read alot about dropDrownList, how to fill the values, get values, dependents dropDownList, the wiki, etc etc… but im not able to finish one thing that im trying to do for almost 2 days…
Imagine this scenario:
I got 1 table (tbl_category) that can have a ‘infinite’ number of categories. For that, i have done the typical struct (id, parent_id, …) where parent_id is linked to id, e.g.
+----+-----------+----------------+
| id | parent_id | name |
+----+-----------+----------------+
| 1 | NULL | Motors | Parent
| 2 | 1 | Cars | Child
| 3 | 2 | Vans | GrandChild
+----+-----------+----------------+
i.e, we have the subsubcategory Vans, that belongs to subcategory Cars and Cars that belongs to category Motors.
Ok, so far so good, but now comes the problem for me…i want to make 3 listBox linked it self, i. e. 1st listBox for categories, 2dn listBox for subcategories and a 3rd listBox for the subsubcategories.
The 1st one is filled when the page is loaded, the second is filled when i clicked on the category and the 3rd is filled only when the user click on the subcategory (2nd listBox). All the examples that i read, works in a different way, i.e. we clicked on the 1st listBox and the others two or more are filled automatically via JSON.
The really problem is when i click on the 2nd, nothing happens…showing some code:
Controller
...
public function accessRules()
{
return array(
...
array('allow',
'actions' => array('create', 'updatechild', 'updategrandchild'),
'users' => array('@'),
),
...
);
}
...
// ListBox LVL 2
public function actionUpdateChild()
{
// Childs
$data = Category::model()->findAll('parent_id=:category_id', array(':category_id' => (int) $_POST['parent_id']));
$data = CHtml::listData($data, 'id', 'name');
//$listBoxChilds = "<option value=''>Select Childs</option>";
foreach($data as $id => $value)
//$listBoxChilds .= CHtml::tag('option', array('value' => $id), CHtml::encode($value), true);
echo CHtml::tag('option', array('value' => $id), CHtml::encode($value), true);
/*
// GrandChilds
$listBoxGrandChilds = "<option value=''>Select Grand Childs</option>";
// return data (JSON)
echo CJSON::encode(array(
'listBoxChilds' => $listBoxChilds,
'listBoxGrandChilds' => $listBoxGrandChilds
));*/
}
// ListBox LVL 3
public function actionUpdateGrandChild()
{
$data = Category::model()->findAll('parent_id=:category_id', array(':category_id' => (int) $_POST['child_id']));
$data = CHtml::listData($data, 'id', 'name');
//$listBoxGrandChilds = "<option value=''>Select Grand Childs</option>";
foreach($data as $id => $value)
//$listBoxGrandChilds .= CHtml::tag('option', array('value' => $id), CHtml::encode($value), true);
echo CHtml::tag('option', array('value' => $id), CHtml::encode($value), true);
// return data (JSON)
/*echo CJSON::encode(array(
'listBoxGrandChilds' => $listBoxGrandChilds
));*/
}
View
...
<div class="row">
<?php echo $form->labelEx($item,'category_id'); ?>
<?php
// Show the parents
echo $form->listBox($item, 'category_id', CHtml::listData(Category::model()->findAll('parent_id IS NULL'), 'id', 'name'),
array(
'ajax' => array(
'type' => 'POST', // request type
'url' => CController::createUrl('item/updatechild'), // url to call
//'dataType' => 'JSON', // data type
'data' => array('parent_id'=>'js:this.value'), // pass the id
'update' => '#'.CHtml::activeId($item, 'category_lv2'), // selector to update
/*'success'=>'function(data) {
$("#'.CHtml::activeId($item, "category_lv2").'").html(data.listBoxLv2);
}',*/
)
));
?>
<?php echo $form->error($item,'category_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($item,'category_id'); ?>
<?php
// Show the childs 'linked' to the previous selected parent
echo $form->listBox($item, 'category_id', array(), array('id' => 'Item_category_lv2'),
array(
'ajax' => array(
'type' => 'POST', // request type
'url' => CController::createUrl('item/updategrandchild'), // url to call
//'dataType' => 'JSON',
'data' => array('child_id'=>'js:this.value'), // pass the id
'update' => '#'.CHtml::activeId($item, 'category_lv3'), // selector to update
/*'success' => 'function(data) {
$("#Item_category_lv3").html(data.dropDownCities);
$("#'.CHtml::activeId($item, "category_lv3").'").html(data.listBoxLv3);
};'*/
)
));
?>
<?php echo $form->error($item,'category_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($item,'category_id'); ?>
<?php
// Show the grandchilds 'linked' to the previous selected childs
echo $form->listBox($item, 'category_id', array(), array('id' => 'Item_category_lv3'));
?>
<?php echo $form->error($item,'category_id'); ?>
</div>
...
With the above code, i managed to fill the 1st and 2nd listBox, but when i clicked on the 2nd nothing happens. Can someone help on this, or maybe give me the right way to archive this?