php Yii2下拉空选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17060177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:11:38  来源:igfitidea点击:

Yii2 dropdown empty option

phpdrop-down-menuyii2

提问by akmnahid

How to implement the following Yii code to Yii2:

如何将以下 Yii 代码实现到 Yii2:

<?php 
   echo $form->dropDownList($model, 
                           'project', 
                           $model->getProjectOptions(), 
                           array('empty' => 'Empty string')
   ); 
?>

回答by ineersa

Why not

为什么不

<?
    dropDownList($model, 
        'project', 
        $model->getProjectOptions(), 
        array('prompt'=>'Empty string')
    ); ?>
  • prompt: string, a prompt text to be displayed as the first option;
  • prompt: 字符串,作为第一个选项显示的提示文本;

Here is old CHtml https://github.com/yiisoft/yii2/blob/master/framework/yii/helpers/base/Html.php

这是旧的 CHtml https://github.com/yiisoft/yii2/blob/master/framework/yii/helpers/base/Html.php

Can find there if you need something more.

如果您需要更多东西,可以在那里找到。

回答by sarin surendran

Use the following code to get the dropdownlist in yii2 friend.

使用以下代码获取yii2朋友中的下拉列表。

<?php 
    //use app\models\Country;
    $countries=Country::find()->all();

    //use yii\helpers\ArrayHelper;
    $listData=ArrayHelper::map($countries,'code','name');

    echo $form->field($model, 'name')->dropDownList(
                                    $listData, 
                                    ['prompt'=>'Select...']);
    ?>

回答by Kshitiz

Try this : You can remove the templet if you want.

试试这个:如果需要,您可以移除模板。

<?php
  $form = ActiveForm::begin([
    'id' => 'test-form',
    'options' => ['class' => 'form-horizontal'],
    'enableClientValidation'=> true,
    'enableAjaxValidation'=> false,
    'validateOnSubmit' => true,
    'validateOnChange' => true,
    'validateOnType' => true,
    'action' => Yii::$app->homeUrl . 'your/url/path'
  ]);
?>

    echo $form->field($model, 
                'your_field_name', 
                ['template' => '<div class="col-md-3">
                                     {label}
                                </div>
                               <div class="col-md-9"> 
                                     {input}{error}{hint}
                               </div>'
                ])
                ->dropdownList($option_array, ['prompt' => '--Select--']);

<?php ActiveForm::end(); ?>

回答by DavidASG

You are looking something like this?

你看起来像这样吗?

<?=$form->field($model, 'project')
        ->dropDownList(ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'))
        ->label(false);
?>

回答by Kevin Dantas

When you make the projectOptions array, just make the first index had a null key, like that:

当你创建 projectOptions 数组时,只需让第一个索引有一个空键,就像这样:

[
  null => 'Empty option',
  ... // Your options
]

And in your view you just add

在您看来,您只需添加

$form->field($model, 'project')->dropDownList($model->projectOptions);

When your function name starts with 'get' followed by an uppercase letter, the yii understand that as an attribute, so

当您的函数名称以 'get' 开头,后跟一个大写字母时,yii 将其理解为一个属性,因此

public function getSomeOptions(){ ..

is the same someOptions, is the same principle of the table relationships

是一样的someOptions,表关系的原理是一样的