我需要在Yii2
中创建一个从属下拉列表我有两个表,admin
和employee
,当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>";
}
}
为所有