How to make a drop down list in yii2?
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) ?>
Yii2 dropdown list selected value
If you need to make a prompt:
<?= $form->field($model, 'test')->dropDownList($items
, 'prompt' => ' -- Select Value --']) ?>
If another scenario
Lets say you chose to select 'B' from your array:
$items =[‘A’,‘B’,‘C’…‘Z’];
the key of 'B' is 1 so you need to do this:
<?= $form->field($model, 'test')->dropDownList($items)
,['options' => [1 => ['Selected'=>'selected']]
, 'prompt' => ' -- Select Value --']) ?>
Creating a dropdown list in Yii2
You could simply try this :
<?= Html::dropDownList('foo', null, [
'fish' => 'Fish',
'dog' => 'Dog',
], ['id' => 'bar', 'prompt'=>'Select Pet Type', 'onchange' => 'run()']) ?>
Or with a form and model :
<?= $form->field($model, 'attribute')->dropDownList([
'fish' => 'Fish',
'dog' => 'Dog',
], ['id' => 'bar', 'prompt'=>'Select Pet Type', 'onchange' => 'run()']) ?>
How to make dropdown list in Yii2 with ArrayHelper
In your case using ArrayHelper
does not make any sense, since you need to process only one record and you know exact structure of required array. So just build it directly from query results:
$model4 = Pendaftar::find()
->select([
'pro1.id_prodi_penerima as id_prodi_pilihan_1',
'pro1.nama_prodi_penerima as prodi_pilihan_1',
'pro2.id_prodi_penerima as id_prodi_pilihan_2',
'pro2.nama_prodi_penerima as prodi_pilihan_2'
])
->from('pendaftar')
->leftJoin('prodi_penerima as pro1', 'pro1.id_prodi_penerima=pendaftar.prodi_pilihan_1')
->leftJoin('prodi_penerima as pro2', 'pro2.id_prodi_penerima=pendaftar.prodi_pilihan_2')
->andWhere(['=', 'id_user_pendaftar', $id])
->asArray()
->one();
$rows = [
$model4['id_prodi_pilihan_1'] => $model4['prodi_pilihan_1'],
$model4['id_prodi_pilihan_2'] => $model4['prodi_pilihan_2'],
];
return $this->renderAjax('lulus_ujian', [
'model' => $this->findModel($id),
'rows' => $rows,
]);
yii2 htmlhelper dropdownlist values
You have:
echo Html::dropDownList("Listname","",ArrayHelper::map($array,'id','value'),['prompt' => '--- select ---']) ;
If you want the same value for each input as its content, it should be:
echo Html::dropDownList("Listname","",ArrayHelper::map($array,'value','value'),['prompt' => '--- select ---']) ;
For more info, take a look at the official ArrayHelper::map documentation: http://www.yiiframework.com/doc-2.0/yii-helpers-basearrayhelper.html#map()-detail
You have to keep in mind that Html::dropDownList is expecting a key=>value array, how you get that array doesn't matter. I mean, ArrayHelper is just, as the name suggest, a helper. You can build your array by your own if you feel more confortable.
How to make dropdown list from two tables in DB in Yii2?
You have to run first a query to get data for the dropdown, something similar as that, assumed that the field of the translated text in translate
is text
:
$depart = (new \yii\db\Query())
->select('d.id, t.text')
->from(['d' => 'depart', 't' => 'translate'])
->where('d.title_id = t.id')
->orderBy('t.text')
->all();
Other possibility to get the same data with ActiveRecord, assumed that the association from the model Depart
to Translate
is defined in model Depart
.
$depart = Depart::find()
->join('translate')
->select('depart.id, translate.text')
->orderBy(translate.text)
->asArray()
->all();
In model Depart
:
/**
* @return \yii\db\ActiveQuery
*/
public function getTranslate()
{
return $this->hasOne(Translate::class, ['id' => 'title_id']);
}
Than you can use your form field with text
insted of title_id
:
<?= $form->field($model, 'departId')->dropDownList(ArrayHelper::map($depart, 'id', 'text'), ['prompt' => 'Choose department',] ); ?>
DropDownList yii 2.0 example
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);
::
?>
How to set value in drop down list yii2
You can use this way :
City model
public static function getCityByCountry($countryId)
{
$cities = self::find()
->where(['country_id' => $countryId])
->select(['name'])
->indexBy('id')
->orderBy(['name' => SORT_ASC])
->column();
return $cities;
}
Form.php
<?= $form->field($organization, 'city_id')->dropDownList(City::getCityByCountry($organization->country_id), ['prompt' => '---']) ?>
Do the same for country_id
.
You can call same function in your jquery also to get city of selected country.
Related Topics
Checking Browser's Language by PHP
How to Enable Put Requests in Azure
Multi Checkbox Fields in Woocommerce Backend
Destroy Session on Window Close
Upload Doesn't Work Right When the File Is Too Big
How to Create Clean Url Using .Htaccess
How to Stop PHP Sleep() Affecting My Whole PHP Code
How to Generate an .Xlsx Using PHP
Storing Time Information: Timezone Required
Populate Select Drop Down from a Database Table
Can Anyone Give Me an Example for PHP's Curlfile Class
PHP Session with an Incomplete Object
PHP Date Time Greater Than Today
PHP Notice: Undefined Offset: 1 with Array When Reading Data