How to make a drop down list in yii2?

Dency G B picture Dency G B · Feb 5, 2014 · Viewed 202.2k times · Source

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?

Answer

Dency G B picture Dency G B · Feb 5, 2014

It is like

<?php
use yii\helpers\ArrayHelper;
use backend\models\Standard;
?>

<?= Html::activeDropDownList($model, 's_id',
      ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>

ArrayHelper in Yii2 replaces the CHtml list data in Yii 1.1.[Please load array data from your controller]

EDIT

Load data from your controller.

Controller

$items = ArrayHelper::map(Standard::find()->all(), 's_id', 'name');
...
return $this->render('your_view',['model'=>$model, 'items'=>$items]);

In View

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