I am using Yii 2 basic application template. I am using dynamic form widget in Yii 2. I am using nested dynamic form scenario for creating the form.
The user selects the ASC from the drop down and selects the time and adds many students details for each time. I want to create a dependent drop down where when the user selects the ASC from the drop down list then students names should be changed in the corresponding student drop down field. I have created a function which displays the student names based on selecting the ASC
public function actionStudentLists($id)
{
$countstudents = Student::find()->where(['ASCId' => $id])->count();
$Students = Student::find()->where(['ASCId' => $id])->all();
if ($countstudents > 0) {
echo "<option value>Select Students</option>";
foreach ($Students as $Student) {
ob_start();
echo "<option value='" . $Student->StudentId . "'>" . $Student->StudentName . "</option>";
}
} else {
echo "<option> - </option>";
}
}
form
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\jui\DatePicker;
use wbraganca\dynamicform\DynamicFormWidget;
use app\models\Ascassignment;
use app\models\Asccenter;
use kartik\time\TimePicker;
use yii\helpers\ArrayHelper;
use app\models\Student;
/* @var $this yii\web\View */
/* @var $model app\models\Ascteacherreport */
/* @var $form yii\widgets\ActiveForm */
?>
<script>
$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
$("#dynamic-form")[0].reset();
</script>
<div class="ascteacherreport-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form', 'options' => ['class' => 'disable-submit-buttons'],
]);?>
<div class="panel panel-primary " >
<div class="panel panel-heading"><font size="3"><b>Teacher Report</b></font></div>
<div class="row">
<div class="col-sm-4">
<?= $form->field($model, 'ASCId')->dropDownList(ArrayHelper::map(Asccenter::find()->leftJoin('ascassignment','`ascassignment`.`ASCId`=`asccenter`.`ASCId`')->where(['ascassignment.UserId' => \Yii::$app->user->identity->getonlyid()])->all(),'ASCId','ASCName'), ['prompt' => 'Select ASC Center','onChange' => '
$.post("index.php?r=student/student-lists&id=' . '"+$(this).val(),function(data){
});']) ?>
</div>
<div class="col-sm-4">
<?= $form->field($model, 'DateofReport')->widget(DatePicker::classname(), [
//'language' => 'ru',
'dateFormat' => 'yyyy-MM-dd',
'options' => ['class' => 'form-control picker','readOnly'=>'readOnly'],
'clientOptions'=>['changeMonth'=>true,
'changeYear'=>true,
'readOnly'=>true]
]) ?>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading"><font size="3"><b>Time and Student Details</b></font></div>
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper',
'widgetBody' => '.container-items',
'widgetItem' => '.time-item',
'limit' =>10,
'min' => 1,
'insertButton' => '.add-time',
'deleteButton' => '.remove-time',
'model' => $modelsTime[0],
'formId' => 'dynamic-form',
'formFields' => [
'Time',
],
]); ?>
<table class="table table-bordered">
<thead>
<tr bgcolor='#B8B8B8'>
<th style='border: 1px solid black;'></th>
<th class ="text-center" style='border: 1px solid black;'>Time</th>
<th class ="text-center" style='border: 1px solid black;'>Student Details</th>
<th class="text-center" style='border: 1px solid black;'>
<button type="button" class="add-time btn btn-success btn-xs"><span class="fa fa-plus"></span> Add Time</button>
</th>
</tr>
</thead>
<tbody class="container-items">
<?php foreach ($modelsTime as $indexTime => $modelTime): ?>
<tr class="time-item">
<td class="vcenter" style='border: 1px solid black;'>
<?php
// necessary for update action.
if (! $modelTime->isNewRecord) {
echo Html::activeHiddenInput($modelTime, "[{$indexTime}]ASCReportDetailsId");
}
?>
</td>
<td style='border: 1px solid black;width:15%'>
<?php
if (! $modelTime->isNewRecord)
{
$time1 = explode('To',$modelTime->Time);
$fromtime=$time1[0];
$totime = $time1[1];
$modelTime->FromTime = $fromtime;
$modelTime->ToTime = $totime;
echo $form->field($modelTime, "[{$indexTime}]FromTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
echo "<br>";
echo $form->field($modelTime, "[{$indexTime}]ToTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
}
else
{
echo $form->field($modelTime, "[{$indexTime}]FromTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
echo "<br>";
echo $form->field($modelTime, "[{$indexTime}]ToTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
}?>
</td>
<td style='border: 1px solid black;'>
<?= $this->render('_form-studentdata', [
'form' => $form,
'indexTime' => $indexTime,
'modelsStudentdata' => $modelsStudentdata[$indexTime],
]) ?>
</td>
<td class="text-center vcenter" style='border: 1px solid black;'>
<button type="button" class="remove-time btn btn-danger btn-xs"><span class="fa fa-minus"></span></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php DynamicFormWidget::end(); ?>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?= Html::resetButton('Reset',['class' => 'btn btn-default'])?>
</div>
<?php ActiveForm::end(); ?>
</div>
Inner student form
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
//use nex\datepicker\DatePicker;
use yii\web\UploadedFile;
use wbraganca\dynamicform\DynamicFormWidget;
use app\models\Student;
?>
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_student',
'widgetBody' => '.container-student',
'widgetItem' => '.student-item',
'limit' => 50,
'min' => 1,
'insertButton' => '.add-student',
'deleteButton' => '.remove-student',
'model' => $modelsStudentdata[0],
'formId' => 'dynamic-form',
'formFields' => [
'StudentId',
'Subject',
'Topic',
'Confidence',
],
]); ?>
<table class="table table-bordered">
<thead>
<tr bgcolor='#B8B8B8'>
<th style='border: 1px solid black;'></th>
<th class ="text-center" style='border: 1px solid black;'>Student</th>
<th class ="text-center" style='border: 1px solid black;'>Subject</th>
<th class ="text-center" style='border: 1px solid black;'>Topic</th>
<th class ="text-center" style='border: 1px solid black;'>Confidence</th>
<th class="text-center" style='border: 1px solid black;'>
<button type="button" class="add-student btn btn-success btn-xs"><span class="glyphicon glyphicon-plus"></span> Add Student</button>
</th>
</tr>
</thead>
<tbody class="container-student">
<?php foreach ($modelsStudentdata as $indexStudent => $modelStudentdata): ?>
<tr class="student-item">
<td class="vcenter" style='border: 1px solid black;'>
<?php
// necessary for update action.
if (! $modelStudentdata->isNewRecord) {
echo Html::activeHiddenInput($modelStudentdata, "[{$indexTime}][{$indexStudent}]ASCTeacherReportTimeDetailsId");
}
?>
</td>
<td style='border: 1px solid black; width:30%'>
<?= $form->field($modelStudentdata, "[{$indexTime}][{$indexStudent}]StudentId")->label(false)->dropDownList(ArrayHelper::map(Student::find()->leftJoin('asccenter','`asccenter`.`ASCId`=`student`.`ASCId`')->leftJoin('ascassignment','`ascassignment`.`ASCId`=`asccenter`.`ASCId`')->where(['ascassignment.UserId' => \Yii::$app->user->identity->getonlyid()])->all(),'StudentId','StudentName'), ['prompt' => 'Select Student']) ?>
</td>
<td style='border: 1px solid black; width:30%'>
<?= $form->field($modelStudentdata, "[{$indexTime}][{$indexStudent}]Subject")->label(false)->textInput(['maxlength' => true]) ?>
</td>
<td style='border: 1px solid black; width:30%'>
<?= $form->field($modelStudentdata, "[{$indexTime}][{$indexStudent}]Topic")->label(false)->textInput(['maxlength' => true]) ?>
</td>
<td style='border: 1px solid black; width:30%'>
<?= $form->field($modelStudentdata, "[{$indexTime}][{$indexStudent}]Confidence")->label(false)->textInput(['maxlength' => true]) ?>
</td>
<td class="text-center vcenter" style=" border: 1px solid black;width: 90px;">
<button type="button" class="remove-student btn btn-danger btn-xs"><span class="glyphicon glyphicon-minus"></span></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php DynamicFormWidget::end(); ?>