在ajax请求后,我从控制器获取了一个数组作为响应,如何在下拉列表中使用此数组值?
在我看来我有
echo CHtml::dropDownList('client_id', '',CHtml::listData($model,'client_id','client_name'), array(
'ajax'=> array(
'type'=>'POST',
'url'=>Yii::app()->baseUrl.'/index.php?r=page/dynamicDropdownList',
'update'=>'#program_id',
'empty'=>'-Select a Client-')));
// I need to populate the response array in this dropdownlist
echo CHtml::dropDownList('program_id','', CHtml::listData($result,'program_id', 'program_name'));
在我的控制器中
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,
));
}
}
现在我完成了所有工作,但第二个下拉列表也显示了第一个下拉列表的值和结果。
解决方案: - 我已经解析了响应并将其显示在下拉列表中
答案 0 :(得分:1)
这就是你的控制器的样子:
public function actionIndex() {
$model = new SearchForm();
if ($_GET['SearchForm']) {
$model->attributes = $_GET['SearchForm'];
}
//here is where you put your criteria or query commands
if ($_GET['ajax']) {
$this->renderPartial('index', array(
'model' => $model,
));
} else {
$this->render('index', array(
'model' => $model,
));
}
}
您从ajax或GET中捕获数据并使用它执行某些操作,然后将数据传递给视图;
这应该是vies包含的内容:
<? echo $form->dropDownList($model, 'position_type', CHtml::listData(PositionType::model()->findAll(array('condition' => 'status=1')), 'id', 'name'), array('class' => 'postdropdown2', 'empty' => array(-1 => 'All'), 'onchange' => 'showDiv(this.value,1);')); ?>
你必须使用控制器中使用ajax接收的数据,因为它在视图中不可见,如果它没有使用render或renderpartial传递