DropDownList yii 2.0 example

不羁岁月 提交于 2019-12-05 05:32:29
Barry

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);
::
?>
olchick

If you use ActiveForm widget use this:

<?php 
    $items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
    $form->field($model, 'attribute')->dropDownList($items)
?>
Sadia Naseeba

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...']) ?>
<?= $form->field($model, 'name_of_field')->dropdownList(['1' => 'aaa', '2' => 'bbb'], ['prompt' => '---Select Data---']) ?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!