I have refered the wiki and am using the same logic for my problem. My code is
_form code:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'footprint-state-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'state_name'); ?>
<?php
$state_var = new CDbCriteria;
$state_var->order = 'state_name ASC';
?>
<?php echo $form->dropDownList($model,'Footprint_State_id',CHtml::listData(FootprintState::model()->findAll(array('group'=>'state_name')),'Footprint_State_id','state_name'),
array('ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('FootprintStateController/dynamiccounty'),
'update'=>'#county_id'
)
));
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('county_id','', array());
?>
<?php //echo $form->error($model,'Footprint_State_id'); ?>
</div>
controller code:
public function actionDynamiccounty()
{
$data=footprintCounty::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['Footprint_State_id']));
$data=CHtml::listData($data,'Footprint_County_id','county');
foreach($data as $id=>$value)
{
echo CHtml::tag('option',
array('value'=>$id),CHtml::encode($value),true);
}
}
I get the first dropdownlist populated but on selection nothing is populated in the second dropdownlist. I get an empty dropdown. I browsed through many forum post but found no solution. I feel that the function dynamiccounty in the controller is not called by the first dropdownlist. Why? Not able to resolve. Please help me.
Add a dummy div <div id=“testController”></div> and try updating that one. In your controller, comment out everything and just echo ‘Test’. That should tell you if your controller is being called. Also, please verify you have added ActionDynamicCountry to your Controller AccessRules.
View:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'footprint-state-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'state_name'); ?>
<?php
$state_var = new CDbCriteria;
$state_var->order = 'state_name ASC';
?>
<?php echo $form->dropDownList($model,'Footprint_State_id',CHtml::listData(FootprintState::model()->findAll(array('group'=>'state_name')),'Footprint_State_id','state_name'),
array('ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('FootprintStateController/dynamiccounty'),
'update'=>'#testController'
)
));
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('county_id','', array());
?>
<?php //echo $form->error($model,'Footprint_State_id'); ?>
</div>
<div id="testController"></div>
Controller:
public function actionDynamiccounty()
{
/*$data=footprintCounty::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['Footprint_State_id']));
$data=CHtml::listData($data,'Footprint_County_id','county');
foreach($data as $id=>$value)
{
echo CHtml::tag('option',
array('value'=>$id),CHtml::encode($value),true);
}*/
echo 'Test';
}
When i update=#testController, it displays the ‘Test’ from Controller but as soon as i change update to #county_id it does not go to the controller. What does it mean?
I want to show all employees in selected department…
i.e department is selected in first drop down and second dropdown should show employees in that department…
Here’s code…
<div class="row">
<?php
$list=CHtml::listData(Department::model()->findAll(),'department_id','department_name');
echo CHtml::dropDownList('department_id','', $list,
array(
'empty'=>'Please Select',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Employee/dynamicemployee'), //url to call.
'update'=>'#emp_id', //selector to update
)));
?>
</div>
<div id="row">
<?php
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('emp_id','', array(),array('empty'=>'Select','style'=>'width:120px;'));
?>
</div>
Here is my controller code…
public function actionDynamicemployee()
{
$data=Employee::model()->findAllBySql('select emp_id, first_name from employee where department_id='.$_POST['department_id']);
$data=CHtml::listData($data,'emp_id','first_name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'test-execution-form',
'enableAjaxValidation'=>true,
)); ?>
<?php echo $form->errorSummary($model); ?>
<?php echo CHtml::beginForm(); ?>
<br><br>
<table class="dataGrid">
<tr>
<td><?php echo $form->labelEx($model,'Region_id');?></td>
<td>
<?php
$list=CHtml::listData(Region::model()->findAll(),'id','region');
echo CHtml::dropDownList('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
'data'=>'js:$(this).serialize()',
)));
?>
</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'));
echo CHtml::activeHiddenField($model,'Region_id');
?>
</td>
</tr>
<tr>
<td> </td>
<td ><?php echo CHtml::submitButton('Search'); ?></td>
</tr>
</table>
<?php echo CHtml::endForm(); ?>
<?php $this->endWidget(); ?>
</div><!-- form -->
Here is my Controller
public function actionDynamicRegion(){
$data=Lab::model()->findAllBySql('select * from Lab where id>=1 AND Region_id='.$_POST['Region_id']);
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
I want to use activeDropDownList on Region_id. I got the Lab_id.