Yii2 dropDownList 默认值
Yii2 dropDownList Default value
在 yii2 中我有一个下拉列表:
<?= $form->field($model, 'Körperschaft')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'])?>
如何将 'Item B' 设置为默认值?
我明白了!
解决方法是在controller中写:
public function actionCreate()
{
$model->Körperschaft='b';
您可能需要将其放在负责保存的行之后,否则即使用户选择了不同的值,该值也不会改变。下面是一个例子:
public function actionCreate(){
//Something you want to do before saving
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//Something you do after saving before redirecting
return $this->redirect(['your-prefered-page']);
}
// Some other lines of code
$model->Körperschaft='b';
return $this->render('create', [
'model' => $model,
]);
}
我认为这会有所帮助。
试试这个
<?= $form->field($model, 'Körperschaft')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'], ['options'=>['b'=>['Selected'=>true]]])?>
你可以使用下面的代码。这里 b 会默认出现
<?= Html::dropDownList('modelfield', null, ['1' => 'a', '2' => 'b', '3' => 'c'], [ 'class'=>'form-control','prompt' => 'Select Rating', 'options' => [ 2 => ['Selected'=>'selected']] ]);
?>
在 yii2 中我有一个下拉列表:
<?= $form->field($model, 'Körperschaft')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'])?>
如何将 'Item B' 设置为默认值?
我明白了! 解决方法是在controller中写:
public function actionCreate()
{
$model->Körperschaft='b';
您可能需要将其放在负责保存的行之后,否则即使用户选择了不同的值,该值也不会改变。下面是一个例子:
public function actionCreate(){
//Something you want to do before saving
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//Something you do after saving before redirecting
return $this->redirect(['your-prefered-page']);
}
// Some other lines of code
$model->Körperschaft='b';
return $this->render('create', [
'model' => $model,
]);
}
我认为这会有所帮助。
试试这个
<?= $form->field($model, 'Körperschaft')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'], ['options'=>['b'=>['Selected'=>true]]])?>
你可以使用下面的代码。这里 b 会默认出现
<?= Html::dropDownList('modelfield', null, ['1' => 'a', '2' => 'b', '3' => 'c'], [ 'class'=>'form-control','prompt' => 'Select Rating', 'options' => [ 2 => ['Selected'=>'selected']] ]);
?>