I am using select2 extension by kartik-v. I was able to make the department dependent on the faculty, but am having a problem making the courses dependent on the department. It shows on the courses list in the courses table in the database.
Here is my view file code:
<?php
$data = ArrayHelper::map(Faculty::find()->all(), 'id', 'facultyname');
echo $form->field($model, 'faculty_id')->widget(Select2::classname(), [
'data' => $data,
'language' => 'en',
'options' => ['prompt'=>'select faculty',
'placeholder' => 'Select Faculty.....',
'onChange' => '$.post("'.Url::to(['/department/lists']).'?id=' . '"+$(this).val(), function(data){
$("select#student-department_id").html(data);
});'
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?php
echo $form->field($model, ‘department_id’)->widget(Select2::classname(), [
'language' => 'en',
'options' => ['prompt'=>'....Select Department....',
'placeholder' => 'Select Department...',
'onChange' => '$.post("'.Url::to(['course/lists']).'?id=' . '"+$(this).val(), function(data){
$("select#student-course_id").html(data);
});'
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?php
$courseList = ArrayHelper::map(Course::find()->orderby('coursename')->asArray()->all(), 'id',
'coursename');
echo $form->field($model, 'course_id')->widget(Select2::classname(), [
'data' => $courseList,
'language' => 'en',
'size' => Select2::MEDIUM,
'options' => ['prompt'=>'....Select Courses....','placeholder' => 'Select Courses...','multiple' => true,'selected' => 'selected'],
'pluginOptions' => ['allowClear' => true],
]);
?>
My Course Controller code;
public function actionLists($id)
{
$countCourses = Course::find()
->where(['department_id' => $id])
->count();
$Courses = Courses::find()
->where(['department_id' => $id])
->all();
if($countCourses>0){
foreach($Courses as $Course){
echo "<option value='".$Course->id."'>".$Course->coursename."</option>";
}
}
else{
echo "<option>-</option>";
}
}