0

could someone tell me how to create a selected value in dropdown list?

Here is my dropdown list:

       <?= Html::dropDownList(
        'calculation-type',
        $calculateByMonths,
        $calculationTypeList, [
        'options' => [
            Employee::DISABLED =>[
                'disabled' => true,
                'selection' => true
            ]
        ],
        'id' => 'calculation-type',
    ]); ?>

That line selection => true doesn't work, I don't know why :( Thanks for the help.

user7435747
  • 417
  • 2
  • 5
  • 11
  • Possible duplicate of [DropDownList yii 2.0 example](http://stackoverflow.com/questions/26594074/dropdownlist-yii-2-0-example) – wormi4ok Apr 13 '17 at 15:36

1 Answers1

0

As you can see in official Yii2 documentation second argument in Html::dropDownList is $selection, and it has to contain string or array of selected values.

Values in dropDownList are keys in $items array. For example, if you have an array of months and you need to make February selected:

<?php

$month = [
    'jan' => 'January',
    'feb' => 'February',
    'mar' => 'March',
    'apr' => 'April',
];

echo \yii\helpers\Html::dropDownList(
    'months', //name of your select tag
    'feb', // selected option value
    $month // array of items
);
?>

<!-- Output of the dropDownList-->
<select name="months">
    <option value="jan">January</option>
    <option value="feb" selected>February</option>
    <option value="mar">March</option>
    <option value="apr">April</option>
</select>
wormi4ok
  • 602
  • 7
  • 11