我需要在Yii2中创建一个依赖的下拉列表
我有两个表admin
和employee
,admin
选择employee
名称,employee's
详细信息,例如电子邮件 ,年龄, dateofbirth 和手机应自动填充。
我该怎么做?
答案 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>";
}
}
为所有人做这件事