Yii2 中的相关下拉列表在更新活动表单时重置值 [英] Dependent Dropdown in Yii2 making values reset while updating the Active form

查看:15
本文介绍了Yii2 中的相关下拉列表在更新活动表单时重置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我的问题是我有一个表格.因为我有依赖下拉列表.例如,如果我选择公司名称,它会自动选择相关公司电子邮件和公司电话号码.这在创建时完美运行.但问题是在更新相同的表单时,依赖值被重置.所以这让我每次都选择公司名称,但我不会那样.一次,如果我在创建值时选择该值,则更新时也不应更改.

Here my problem is i have a form . In that i have dependent dropdown. for example if i select company name it automatically selects the dependent company email and company phone number. This is working perfectly while creating. but the problem is while am updating the same form, the dependent value getting reset. so that makes me to select the company name for every time but i don't to be like that. once if i select the value while creating the value shouldn't change while updating also.

_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
                ],
            ]); ?>

这是控制器代码控制器.php

This is 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>";
        }

    }

推荐答案

最后我找到了答案,实际上错误是我的,因为这里使用的是基于 company_id 的记录,但这里我使用了批准详细信息表 id,所以这是错误,在我将其更改为 company_id 后,现在运行良好

Finally i found answer, actually mistake was mine, because here am using getting records based on company_id but here i have used approvaldetails table id so that is the mistake, after i changed it to company_id, now its working good

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>";
        }

    }

这篇关于Yii2 中的相关下拉列表在更新活动表单时重置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆