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?
I would highly recommend to use Select2 from krajee it has all the options that you might need, using multiple in dropDownList() will make you able to choose multiple values but by pressing and holding Ctrl and choosing which is not comfortable as choosing them using Select2.
If you use $model as the model for the dropdownlist you'll save the ID of the selected value to the database. So then when you're going to update the record, the $model->prj_id will be set to the saved value, so that is the value it will display.
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) ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With