DropDownList yii 2.0 example
Asked Answered
S

4

7

I am using yii 2.0 Framework. How i can make options from my database. I found this, but it's yii 1.1:

<?php echo CHtml::dropDownList('listname', $select, 
          array('M' => 'Male', 'F' => 'Female'));

I want to pass it to form:

<?php $form->dropDownList() ?>

How i can fill my dropdownlist from my database table?

Skijoring answered 27/10, 2014 at 18:12 Comment(0)
D
11

Use yii\helpers\Html it contains Html::dropDownList().

echo Html::dropDownList('listname', $select, ['M'=>'Male', 'F'=>'Female']);

Check Yii Framework 2.0 API

Controller

public function actionSomething() {
    $sexes = ['M'=>'Male', 'F'=>'Female'];  
    $this->render('yourView', ['sexes'=>$sexes]);
}

View

<?php
::
    echo Html::dropDownList('listname', $select, $sexes);
::
?>
Drambuie answered 27/10, 2014 at 18:26 Comment(4)
I am using ActiveForm widget, is it possible to pass parameters there? What is the best way? Create and array in controller, I am out of english examples (:Skijoring
You can create the array in the controller, but I usually create the array in the view. Are the dropdown values stored in the database or is it just an unstored list of values?Drambuie
Yes, values are stored in database. Can you make an example of how to pass them from controller?Skijoring
I see in you question that the select values come from a database tabel. Create a model with a method .f.i. getSexes() that return the array.Drambuie
O
13

If you use ActiveForm widget use this:

<?php 
    $items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
    $form->field($model, 'attribute')->dropDownList($items)
?>
Orbit answered 4/11, 2014 at 18:42 Comment(0)
D
11

Use yii\helpers\Html it contains Html::dropDownList().

echo Html::dropDownList('listname', $select, ['M'=>'Male', 'F'=>'Female']);

Check Yii Framework 2.0 API

Controller

public function actionSomething() {
    $sexes = ['M'=>'Male', 'F'=>'Female'];  
    $this->render('yourView', ['sexes'=>$sexes]);
}

View

<?php
::
    echo Html::dropDownList('listname', $select, $sexes);
::
?>
Drambuie answered 27/10, 2014 at 18:26 Comment(4)
I am using ActiveForm widget, is it possible to pass parameters there? What is the best way? Create and array in controller, I am out of english examples (:Skijoring
You can create the array in the controller, but I usually create the array in the view. Are the dropdown values stored in the database or is it just an unstored list of values?Drambuie
Yes, values are stored in database. Can you make an example of how to pass them from controller?Skijoring
I see in you question that the select values come from a database tabel. Create a model with a method .f.i. getSexes() that return the array.Drambuie
A
4

Yes if you use ActiveForm widget , u dont have to change anything in the controller , in views, in the form, add this where u want the dropdown

    use yii\helpers\ArrayHelper;

    <?php 
        $city = \app\models\City::find()->all(); 
        $listData=ArrayHelper::map($city,'cityId','cityName'); 
    ?>    
    <?= $form->field($model, 'cityId')->dropDownList($listData,['prompt'=>'Choose...']) ?>
Asymptomatic answered 14/7, 2015 at 8:53 Comment(0)
S
2
<?= $form->field($model, 'name_of_field')->dropdownList(['1' => 'aaa', '2' => 'bbb'], ['prompt' => '---Select Data---']) ?>
Salomie answered 14/7, 2015 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.