最后我做到了,就是这样。
在我的控制器中,
public function actionDynamicDropdownList()
{
if($_POST['client_id'] > '0') {
$result = Yii::app()->db->createCommand()->select('program_id, program_name')->from('program')->where('client_id='.$_POST['client_id'].'')->order('program_name')->queryAll();
$this->render('admin', array(
'result' => $result,
));
}
}
public function actionDynamicProgramsList()
{
if($_POST['program_id'] > '0') {
$results = Yii::app()->db->createCommand()->select('ad_id, ad_name')->from('ads')->where('program_id='.$_POST['program_id'].'')->order('ad_name')->queryAll();
$this->render('admin', array(
'results' => $results,
));
}
}
在我看来,我添加了
<?php
$data = Yii::app()->db->createCommand()->select('client_id, client_name')->from('client')->order('client_name')->queryAll();
echo CHtml::dropDownList('client_id', '',CHtml::listData($data,'client_id','client_name'), array(
'empty'=>'- Select Client Name -',
'ajax'=> array(
'type'=>'POST',
'url'=>Yii::app()->baseUrl.'/index.php?r=page/dynamicDropdownList',
'update'=>'#program_id')));
//empty since it will be filled by the above dropdown
echo CHtml::dropDownList('program_id','', CHtml::listData($result,'program_id','program_name'), array(
'empty'=>'- Select Program Name - ',
'onchange'=>'alert(this.value);',
'ajax'=> array(
'type'=>'POST',
'url'=>Yii::app()->baseUrl.'/index.php?r=page/dynamicProgramsList',
'update'=>'#ad_id')));
//empty since it will be filled by the above dropdown
echo CHtml::dropDownList('ad_id','', CHtml::listData($results,'ad_id','ad_name'), array(
'empty'=>'- Select Ad Name - ',
));
?>