dpg98445 2015-11-13 07:06
浏览 58
已采纳

更新时,从属下拉列表未显示选定的值

I am new to yii. I have a dependent dropdown, my problem is that in dependent dropdown when someone is editing, while editing the dropdown is not automatically selected. Here is my form code:

  <div class="row">

  <?php 
Yii::app()->clientScript->registerScript('courseDropdown','jQuery(function($) {
            $("#Subject_Subjectid").trigger("change"); 
            $("#Subjectcourse_CourseId").val(\''.$model->CourseId.'\');
    });
');//write this code on _form.php file
?>

            <?php echo $form->labelEx($model,'Subjectid'); ?>       
            <?php 

             $sub = CHtml::listData(Subject::model()->findAll(array("condition"=>"School_Id='$School' and Status=1")),'Subjectid','SubjectName');

             echo CHtml::activeDropDownList($model,'Subjectid',CHtml::listData(Subject::model()->findAll(array("condition"=>"School_Id='$School' and Status=1")),'Subjectid','SubjectName'),
             array(
             'empty'=>'--Select a Subject--',
             'ajax' => array(
             'type'=>'POST', //request type
             'url'=>CController::createUrl('Timetable/subjectid'), //url to call.
             'data'=>array('Subjectid'=>'js: $(this).val()'),         
             'update'=>'#CourseId', //selector to update       
             )));
            echo $form->error($model,'Subjectid');

             echo $form->labelEx($model,'CourseId');
             echo CHtml::dropDownList('CourseId','', array(), array('empty' => '-- Select a Course --'));
             echo $form->error($model,'CourseId');

     ?>
    </div> 

This is my controller action

public function actionSubjectid()
    {               
        $SchoolId=Yii::app()->session['Schoolid'];
        $subjectid=$_POST['Subjectid'];
        $subject = Subject::model()->findByPk($subjectid);      
        $data = Subjectcourse::model()->findAll(array("order"=>"CourseName ASC", "select"=>"CourseId,CourseName","condition" => "SubjectId='$subjectid' AND Status=1 AND School_Id='$SchoolId'"));
        $data=array('empty'=>'-- Select a Course --') +CHtml::listData($data,'CourseId','CourseName');      
        foreach($data as $value=>$name)
        {
            echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
        }
    }

This is my action update

public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Timetable']))
        {
            $model->attributes=$_POST['Timetable'];
            $model->School_Id=Yii::app()->session['Schoolid'];
            $CourseId=$_POST['CourseId'];
            if($CourseId=="empty")
                   $model->CourseId='';
            else
                $model->CourseId=$_POST['CourseId'];           


            $model->Status=1;
            if($model->save())
                $this->redirect(array('view','id'=>$model->Id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }
  • 写回答

1条回答 默认 最新

  • doudouji2016 2015-11-13 08:37
    关注

    As your second drop down will only work when a change event occurs in your first drop down, you can trigger this event when the page loads, something like this:

    <?php 
    Yii::app()->clientScript->registerScript('courseDropdown','jQuery(function($) {
                $("#Subject_Subjectid").trigger("change"); 
                $("#Subjectcourse_CourseId").val(\''.$model->CourseId.'\');
        });
    ');//write this code on _form.php file
    ?>
    

    Edit: Alternatively, you could populate the second dropdown by querying data from the value of first dropdown:

    if(!$model->isNewRecord) {
        $courseArr = Chtml::listData(Subjectcourse::model()->findAllByAttributes(array('SubjectId'=>$model->Subjectid)), 'CourseId','CourseName'); //your query, modify according to need
    } else {
        $courseArr = array();
    }
    
    echo $form->labelEx($model,'CourseId');
    echo CHtml::dropDownList('CourseId','', $courseArr, array('empty' => '-- Select a Course --'));
    echo $form->error($model,'CourseId');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作