Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a drop down list in yii2?

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?

like image 255
Dency G B Avatar asked Feb 05 '14 05:02

Dency G B


People also ask

How do I select multiple values in a dropdown in Yii2?

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.

How can I show the selected value of DropDownList in Yii?

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.


1 Answers

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) ?> 
like image 185
Dency G B Avatar answered Oct 07 '22 14:10

Dency G B