- Edited
I have a situation where the user selects an ASC center from the drop-down list. All the students belonging to that particular ASC center is returned from the server and should be populated in all the nested dynamic form input field.
It correctly gets populated for the first row, but as the user adds new students and new times, the populated data gets cleared and every time the user needs to select the ASC center for the data to be populated in the newly added students rows. I have assigned a class i to the Student field in the nested form. I have used each function of the jquery which populates all the fields having the class i with the data from the server.
The below image shows the students populated for the first student row on selecting the ASC center
![https://pasteboard.co/PDm9UwYMK4CI.png]
Below is the image which shows the Students input field gets cleared when a user adds new student or adds new time and hence user always has to select ASC center to populate all the student's input with the data.
![https://pasteboard.co/fzUipa4ZYtCO.png]
Below is the function which returns the students for selected ASC
public function actionStudentLists($id)
{
$countstudents = Student::find()->where(['ASCId' => $id])->count();//Counts number of students
$Students = Student::find()->where(['ASCId' => $id])->all(); // Execute students for ASCId
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>";
}
}
Below is the _form.php (snippet)
<?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;
?>
<script>
$(function () {
$(".dynamicform_student").on("beforeInsert", function(e, item) {
$.post("index.php?r=student/student-lists&id='.'"+$("select#ascteacherreport-
ascid").val(),function(data){
$(".i").html(data);
});
});
});
$(function () {
$(".dynamicform_wrapper").on("beforeInsert", function(e, item) {
$.post("index.php?r=student/student-lists&id=' . '"+$("select#ascteacherreport-
ascid").val(),function(data){
$(".i").html(data);
});
});
});
</script>
<?= $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','class'=>'form-
control ascid','onChange' => '
$.post("index.php?r=student/student-lists&id=' . '"+$(this).val(),function(data){
$(".i").each(function()
{
$(".i").html(data); // Populate the StudentId field having the class .i with the data
});
});
'
]) ?>
Below is the nested inner form code which has Student field and class assigned
<?= $form->field($modelStudentdata, "[{$indexTime}][{$indexStudent}]StudentId")-
>label(false)->dropDownList(ArrayHelper::map(Student::find()-
>all(),'StudentId','StudentName'),['prompt' => 'Select
Student','class'=>'form-control i']) ? // class i assigned to StudentId field