nemo
(Nemoneel)
September 9, 2011, 4:06pm
1
Hello,
I try to use dependent dropdown but it’s not working. here is my view and controller. Can you please help me?
View
[code>
<tr>
<td><?php echo CHtml::activeLabel($form,'Region'); ?></td>
<td><?php echo CHtml::dropDownList('Region_id','', CHtml::listData(Region::model()->findAll(),'id','region'),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Rlab'),
'update'=>'#Lab_id',
)));
?>
</td>
</tr>
<tr>
<td><?php echo CHtml::activeLabel($form,'Lab_id'); ?></td>
<td><?php
echo CHtml::activeDropDownList($form,'Lab_id',CHtml::listData(Lab_id::model()->findAll(),'id','name'));?>
</td>
</tr>
</code>
Controller
public function actionRlab()
{
$data = Lab::model()->findAll('Region_id=:parent_id',
array(':parent_id'=>(int) $_POST['Region_id']));
$data = CHtml::listData($data,'id','name');
foreach($data as $id => $value)
{
echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}
}
Try to echo your $_POST[‘Region_id’] in the controller. Does it contain a value? To test, you’ll also need to create a new, empty div with a different Id and update that one.
nemo
(Nemoneel)
September 9, 2011, 4:57pm
3
THe lab value is blank. here is my update view and controller. can you please help
<tr>
<td><?php echo CHtml::activeLabel($form,'Region'); ?></td>
<td><?php echo CHtml::dropDownList('Region_id','', CHtml::listData(Region::model()->findAll(),'id','region'),
array(
'prompt'=>'Please select Region',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Dlab'),
'update'=>'#'.CHtml::activeId($form,'Lab_id'),
)));
?>
</td>
</tr>
<tr>
<td><?php echo CHtml::activeLabel($form,'Lab_id'); ?></td>
<td><?php
echo CHtml::activeDropDownList($form,'Lab_id',array());?>
</td>
</tr>
AND THE CONTROLLER
public function actionDlab()
{
$data = Lab::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['Lab_id']));
$data = CHtml::listData($data,'id','name');
foreach($data as $id => $value)
{
echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}
}
nemo
(Nemoneel)
September 12, 2011, 5:15pm
4
nemo:
THe lab value is blank. here is my update view and controller. can you please help
<tr>
<td><?php echo CHtml::activeLabel($form,'Region'); ?></td>
<td><?php echo CHtml::dropDownList('Region_id','', CHtml::listData(Region::model()->findAll(),'id','region'),
array(
'prompt'=>'Please select Region',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Dlab'),
'update'=>'#'.CHtml::activeId($form,'Lab_id'),
)));
?>
</td>
</tr>
<tr>
<td><?php echo CHtml::activeLabel($form,'Lab_id'); ?></td>
<td><?php
echo CHtml::activeDropDownList($form,'Lab_id',array());?>
</td>
</tr>
AND THE CONTROLLER
public function actionDlab()
{
$data = Lab::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['Lab_id']));
$data = CHtml::listData($data,'id','name');
foreach($data as $id => $value)
{
echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}
}
My dependent dropdown is working but If I use CHTML::activeDropDownList instead as per cookbook http://www.yiiframework.com/wiki/24/ I get validation error that field cannot be blank. Can you tell me
how to solve this
<td>
<?php
$list=CHtml::listData(Region::model()->findAll(),'id','region');
echo CHtml::dropDownList($model,'Region_id',$list,
array(
'empty'=>'Please Select Region',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('test/dynamicRegion'), //url to call.
'update'=>'#Lab_id', //selector to update
)));
?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'Lab_id');?></td>
<td>
<?php
//echo CHtml::activeDropDownList($model,'Lab_id',array());
//empty since it will be filled by the other dropdown
echo CHtml::activeDropDownList($model,'Lab_id', array(),array('id'=>'Lab_id'));
?>
</td>
</tr>