dropDownList 创建时提示,修改时默认选中
2018-02-21 本文已影响0人
duandaoke
创建时提示
controller
use yii\helpers\ArrayHelper;
public function actionCreate()
{
$model = new Customer();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->customer_id]);
} else {
$addresses = Address::find()->all();
$addresses = ArrayHelper::map($addresses,'address_id','address');
return $this->render('create', [
'model' => $model,
'addresses' =>$addresses,
]);
}
}
修改时默认选中
controller
public function actionCreate()
{
$model = new Customer();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->customer_id]);
} else {
$addresses = Address::find()->all();
$addresses = ArrayHelper::map($addresses,'address_id','address');
return $this->render('create', [
'model' => $model,
'addresses' =>$addresses,
]);
}
}
共用 view
<?= $form->field($model,'address_id')->dropDownList($addresses,
['prompt'=>'请选择下面的一项'],
['options' =>
[
$model->address_id => ['selected' => true]
]
]
) ?>