我是yii的新手。我有一个依赖的下拉列表,我的问题是在有人正在编辑的依赖下拉列表中,而不是自动选择编辑下拉列表。 这是我的表单代码:
SELECT DISTINCT *,
SUM(Budget) OVER(PARTITION BY AcctName) AS Budget
FROM (
SELECT
RIGHT(AcctName, CHARINDEX(' ', REVERSE(AcctName))) AS AcctName,
Budget
FROM Your_Table) AS A
这是我的控制器动作
<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>
这是我的行动更新
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);
}
}
答案 0 :(得分:1)
由于您的第二个下拉菜单仅在第一个下拉菜单中发生change
事件时才有效,您可以在页面加载时触发此事件,如下所示:
<?php
Yii::app()->clientScript->registerScript('courseDropdown','jQuery(function($) {
$("#Subject_Subjectid").trigger("change");
$("#Subjectcourse_CourseId").val(\''.$model->CourseId.'\');
});
');//write this code on _form.php file
?>
修改或者,您可以通过查询第一个下拉列表中的数据来填充第二个下拉列表:
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');