我正在研究yii中的依赖下拉列表,我希望部门下拉列表根据我选择的教师自动加载部门,当我选择一个部门时,课程下拉加载课程,代码几乎可以工作,除了下拉列表正在更新自己,而不是下面的下拉菜单。如何让它们正确更新?
这是 视图/站点/ NEWUSER:
`<div class="row">`
<?php echo $form->labelEx($model,'faculty_id'); ?>
<?php echo $form->dropDownList($model,'faculty_id',
CHtml::listData(Faculty::model()->findAll(),'id','faculty_name'),
array(
'prompt'=>'Select Faculty',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loaddepartment'),
'update'=>'#NewUser_faculty_id',
'data'=>array('faculty_id'=>'js:this.value'),)));
?>
<?php echo $form->error($model,'faculty_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'department_id'); ?>
<?php echo $form->dropDownList($model,'department_id',
CHtml::listData(Department::model()->findAll(),'id','department_name'),
array(
'prompt'=>'Select Department',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loadcourses'),
'update'=>'#NewUser_department_id',
'data'=>array('department_id'=>'js:this.value'),)));
?>
<?php echo $form->error($model,'department_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'course_id'); ?>
<?php echo CHtml::dropDownList('department_name','', array(), array(
'prompt'=>'Select Course'
)); ?>
<?php echo $form->error($model,'course_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
这是在控制器中:
public function actionloaddepartment()
{
$data=Department::model()->findAll('faculty_id=:faculty_id',
array(':faculty_id'=>(int) $_POST['faculty_id']));
$data=CHtml::listData($data,'id','department_name');
echo "<option value=''>Select Department</option>";
foreach($data as $value=>$department_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($department_name),true);
}
public function actionloadcourses()
{
$data=Courses::model()->findAll('department_id=:department_id',
array(':department_id'=>(int) $_POST['department_id']));
$data=CHtml::listData($data,'id','course_name');
echo "<option value=''>Select course</option>";
foreach($data as $value=>$course_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($course_name),true);
}
答案 0 :(得分:0)
所以,我得到了一个修复,感谢上帝,在这里,所以任何人都可能需要它
这是新的/ views / site / newuser:
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'first_name'); ?>
<?php echo $form->textField($model,'first_name'); ?>
<?php echo $form->error($model,'first_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'last_name'); ?>
<?php echo $form->textField($model,'last_name'); ?>
<?php echo $form->error($model,'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'faculty_id'); ?>
<?php echo $form->dropDownList($model,'faculty_id',
CHtml::listData(Faculty::model()->findAll(),'id','faculty_name'),
array(
'prompt'=>'Select Faculty',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loaddepartment'),
'update'=>'#department_id',
'data'=>array('faculty_id'=>'js:this.value'
),
)));
?>
<?php echo $form->error($model,'faculty_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'department_id'); ?>
<?php echo CHtml::dropDownList('department_id','', array(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loadcourses'),
'update'=>'#course_id',
'data'=>array('department_id'=>'js:this.value'
),
)))
?>
<?php echo $form->error($model,'department_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'course_id'); ?>
<?php echo CHtml::dropDownList('course_id','', array()
)?>
<?php echo $form->error($model,'course_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
这是新控制器:
public function actionloaddepartment()
{
$data=Department::model()->findAll('faculty_id=:faculty_id',
array(':faculty_id'=>(int) $_POST['faculty_id']));
$data=CHtml::listData($data,'id','department_name');
foreach($data as $value=>$department_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($department_name),true);
}
public function actionloadcourses()
{
$data=Courses::model()->findAll('department_id=:department_id',
array(':department_id'=>(int) $_POST['department_id']));
$data=CHtml::listData($data,'id','course_name');
foreach($data as $value=>$course_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($course_name),true);
}