在Yii2中创建依赖下拉列表

时间:2015-05-29 10:41:04

标签: php yii2

我需要在Yii2中创建一个依赖的下拉列表

我有两个表adminemployeeadmin选择employee名称,employee's详细信息,例如电子邮件年龄 dateofbirth 手机应自动填充。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我希望这会对你有所帮助

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

    }

为所有人做这件事