im working with a dependent dropdown and im getting stuck where i have to populate the first dropdown with data from the db.
here is my form view where the request works and populates the 2nd dropdown
<?php echo CHtml::dropDownList('BUILDING_ID','', array(1=>'CENTRAL ADMIN',2=>'LIBRAY',3=>'ENG BUILDING'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Maintenancerequest/dynamicrooms'),
'update'=>'#roomid', //selector to update
)));
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('roomid','', array());
?>
form view(2) it doesnt work when i populate the dropdown automatically
<?php echo $form->dropDownList($model,'BUILDINGS_BUILDING_ID',
CHtml::listData(Buildings::model()->findAll(),'BUILDING_ID','BUILDING_NAME'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Maintenancerequest/dynamicrooms'),
'update'=>'#roomid', //selector to update
)));
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('roomid','', array());
?>
in the above case the 2nd dropdown does not output anything
controller (i use same for both above cases)
public function actionDynamicrooms()
{
$data=Rooms::model()->findAll('BUILDING_ID=:BUILDING_ID',
array(':BUILDING_ID'=>(int) $_POST['BUILDING_ID']));
$data=CHtml::listData($data,'roomid','roomname');
foreach($data as $value=>$roomname)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($roomname),true);
}
}
i could work with the first case but i have a long list of items and i dont think its good code practice
kindly assist.