How to make a drop down list in yii2?

2019-01-13 02:53发布

How to make a dropdown in yii2 using an activeform and a model? Since all the methods has changed in yii2,how it is done in the new one?

11条回答
欢心
2楼-- · 2019-01-13 03:21

It Seems there are many good answers for this question .So i will try to give a detailed answer

active form and hardcoded data

<?php
    echo $form->field($model, 'name')->dropDownList(['1' => 'Yes', '0' => 'No'],['prompt'=>'Select Option']);
?>

or

<?php
    $a= ['1' => 'Yes', '0' => 'No'];
    echo $form->field($model, 'name')->dropDownList($a,['prompt'=>'Select Option']);
?>

active form and data from a db table

we are going to use ArrayHelper so first add it to the name space by

<?php
    use yii\helpers\ArrayHelper;
?>

ArrayHelper has many use full functions which could be used to process arrays map () is the one we are going to use here this function help to make a map ( of key-value pairs) from a multidimensional array or an array of objects.

<?php
    echo $form->field($model, 'name')->dropDownList(ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>

not part of a active form

<?php
    echo Html::activeDropDownList($model, 'filed_name',['1' => 'Yes', '0' => 'No']) ;
?>

or

<?php
    $a= ['1' => 'Yes', '0' => 'No'];
    echo Html::activeDropDownList($model, 'filed_name',$a) ;
?>

not an active form but data from a db table

<?php
    echo Html::activeDropDownList($model, 'filed_name',ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>
查看更多
可以哭但决不认输i
3楼-- · 2019-01-13 03:26

Following can also be done. If you want to append prepend icon. This will be helpful.

<?php $form = ActiveForm::begin();    
   echo $form->field($model, 'field')->begin();
     echo Html::activeLabel($model, 'field', ["class"=>"control-label col-md-4"]); ?>
       <div class="col-md-5">
          <?php echo Html::activeDropDownList($model, 'field', $array_list, ['class'=>'form-control']); ?>
          <p><i><small>Please select field</small></i>.</p>
          <?php echo Html::error($model, 'field', ['class'=>'help-block']); ?>
       </div>
   <?php echo $form->field($model, 'field')->end(); 
ActiveForm::end();?>
查看更多
Root(大扎)
4楼-- · 2019-01-13 03:29
<?= $form->field($model, 'attribute_name')->dropDownList(
         ArrayHelper::map(Table_name::find()->all(),'id','field_name'),
        ['prompt' => 'Select']
) ?>

This will help you...Don't forget to use the class file in header.

查看更多
\"骚年 ilove
5楼-- · 2019-01-13 03:30

There are some good solutions above, and mine is just a combination of two (I came here looking for a solution).

@Sarvar Nishonboyev's solution is good because it maintains the creation of the form input label and help-block for error messages.

I went with:

<?php
use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?=
$form->field($model, 'parent_id')
     ->dropDownList(
            ArrayHelper::map(Product::find()->asArray()->all(), 'parent_id', 'name')
            )
?>

Again, full credit to: @Sarvar Nishonboyev's and @ippi

查看更多
何必那么认真
6楼-- · 2019-01-13 03:31

Maybe I'm wrong but I think that SQL query from view is a bad idea

This is my way

In controller

$model = new SomeModel();
$items=ArrayHelper::map(TableName::find()->all(),'id','name');


return $this->render('view',['model'=>$model, 'items'=>$items])

And in View

<?= Html::activeDropDownList($model, 'item_id',$items) ?>

Or using ActiveForm

<?php $form = ActiveForm::begin(); ?>
 <?= $form->field($model, 'item_id')->dropDownList($items) ?>
<?php ActiveForm::end(); ?>
查看更多
甜甜的少女心
7楼-- · 2019-01-13 03:32

It seems you've found your answer already but since you mentioned the active form I'll contribute with one more, even if it differs only ever so slightly.

<?php
    $form = ActiveForm::begin();

    echo $form->field($model, 'attribute')
        ->dropDownList(
            $items,           // Flat array ('id'=>'label')
            ['prompt'=>'']    // options
        );

    ActiveForm::end();
?>
查看更多
登录 后发表回答