在yi2中创建依赖的下拉菜单


Creating dependent dropdown in Yii2

我需要在Yii2

中创建一个从属下拉列表

我有两个表,adminemployee,当admin选择employee名称时,employee's的详细信息如电子邮件年龄出生日期电话应自动填写。

我该怎么做呢?

希望这对你有帮助

_form.php

<?= $form->field($model, 'company_id')->widget(Select2::classname(), [
                'data' => ArrayHelper::map(Company::find()->all(),'id','companyname'),
                'language' => 'en',
                'options' => [
                'placeholder' => 'Select a company ...',
                'onchange' => '
                                    $.post( "index.php?r=employee/lists&id='.'"+$(this).val(), function( data ) {
                                      $( "select#admin-employee_email" ).html( data );
                                    }),
                                    $.post( "index.php?r=employee/lists1&id='.'"+$(this).val().split("-")[0], function( data ) {
                                      $( "select#admin-phone" ).html( data );
                                    }),
                                    $.post( "index.php?r=employee/lists1&id='.'"+$(this).val().split("-")[0], function( data ) {
                                      $( "select#admin-age" ).html( data );
                                    }),
                                    $.post( "index.php?r=employee/lists2&id='.'"+$(this).val().split("-")[0], function( data ) {
                                      $( "select#admin-dateofbirth" ).html( data );
                                    });',
                ],
                'pluginOptions' => [
                    'allowClear' => true
                ],
            ]); ?>

controller.php

public function actionLists($id)
    {
        $countEmployee = Employee::find()
                ->where(['id' => $id])
                ->count();

        $employee= Employee::find()
                ->where(['id' => $id])
                ->all();
        if($countEmployee >= 0)
        {
            foreach($employeeas as $emp)
            {
                echo "<option value='".$emp->id."'>".$emp->email."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }
    }

为所有