Yii2中的相关下拉列表在更新活动表单时重置值

时间:2015-05-27 04:53:46

标签: yii2

我的问题是我有一个表格。在那我有依赖下拉。 例如,如果我选择公司名称,它会自动选择从属公司电子邮件和公司电话号码。这在创造时非常有效。但问题是在更新相同的表单时,依赖值会重置。所以这让我每次都选择公司名称,但我不喜欢那样。如果我在创建值时选择了值,则在更新时也不应该更改。

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

这是控制器代码     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 个答案:

答案 0 :(得分:0)

最后我找到了答案,实际上错误是我的,因为这里使用的是基于company_id获取记录但是在这里我使用了grantdetails表id,这是错误,在我将其更改为company_id之后,现在它的工作正常

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

    }