Dependent Dropdown in Yii2 making values ​​reset on active form refresh

Here my problem is that I have a form. In that I have a dependent dropdown menu. for example, if I select a company name, it automatically selects the affiliate company's email address and company phone number. This works great when created. but the problem is when I update the same form, the dependent value gets reset. so it makes me choose the name of the company every time, but I don't want to be that. once, if I select a value when creating a value, it shouldn't change on update either.

_form.php

<?= $form->field($model, 'employee_id')->widget(Select2::classname(), [
                'data' => ArrayHelper::map(Employeedetails::find()->all(),'id','employeecode'),
                'language' => 'en',
                'options' => [
                'placeholder' => 'Select a employeecode ...',
                'onchange' => '
                                    $.post( "index.php?r=employeedetails/lists2&id='.'"+$(this).val().split("-")[0], function( data ) {
                                      $( "select#claimprocess-claim_for" ).html( data );
                                    }),

                                    $.post( "index.php?r=employeedetails/lists3&id='.'"+$(this).val().split("-")[0], function( data ) {
                                      $( "select#claimprocess-email" ).html( data );
                                    }),

                                    $.post( "index.php?r=employeedetails/lists1&id='.'"+$(this).val(), function( data ) {
                                      $( "select#claimprocess-employee_name" ).html( data );
                                    });',
                ],
                'pluginOptions' => [
                    'allowClear' => true
                ],
            ]); ?>

      

This is the controller code controller.php

public function actionLists($id)
    {
        $countEmployeedetails = Employeedetails::find()
                ->where(['company_id' => $id])
                ->count();

        $employeedetails = Employeedetails::find()
                ->where(['company_id' => $id])
                ->all();

        if($countEmployeedetails>0){
            foreach($employeedetails as $employee){
                echo "<option value='".$employee->id."'>".$employee->employeecode."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }

    }

    public function actionLists1($id)
    {
        $countEmployeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->count();


        $employeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->all();

        if($countEmployeedetails >= 0)
        {
            foreach($employeedetails as $employee)
            {
                echo "<option value='".$employee->id."'>".$employee->name."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }

    }

    public function actionLists2($id)
    {

        $countEmployeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->count();


        $employeedetails = Employeedetails::find()
                ->where(['id' => $id])
                ->all();

        if($countEmployeedetails >= 0)
        {
            foreach($employeedetails as $employee)
            {
                // $arr["id"] . '-' . $arr['designation']
                    echo "<option value='".$employee->id. '-' .$employee->name. "'>".$employee->RelationName."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }

    }

      

-1


source to share


1 answer


Finally I found the answer, actually the error was mine because here I am using getting records based on company_id, but here I used assertdetails the table id, so this is a bug, after I changed it to company_id, now it works fine



public function actionLists2($id)
    {

        $countEmployeedetails = Employeedetails::find()
                ->where(['company_id' => $id])
                ->count();


        $employeedetails = Employeedetails::find()
                ->where(['company_id' => $id])
                ->all();

        if($countEmployeedetails >= 0)
        {
            foreach($employeedetails as $employee)
            {
                // $arr["id"] . '-' . $arr['designation']
                    echo "<option value='".$employee->id. '-' .$employee->name. "'>".$employee->RelationName."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }

    }

      

0


source







All Articles