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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀