2017-06-26 63 views
1

我正在研究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

所以,我能得到修復,感謝上帝,這是,所以櫃面任何一個都可能需要它

這是新/視圖/網站/ 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); 
    }