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,
));
}