P粉8019040892023-09-06 14:29:43
To achieve the desired output, where each item in the dropdown is represented by a single <option>
tag with the specified label, value, and style, you need to modify your code as follows:
In your view file, update the customDropDown
function call to correctly pass the items array:
<?= $form->customDropDown($dpForm, 'color', [ [ 'label' => 'red', 'value' => 'red', 'options' => [ 'style' => 'color: red' ] ], [ 'label' => 'blue', 'value' => 'blue', 'options' => [ 'style' => 'color: blue' ] ], ] ); ?>Updated method:
public function customDropdown($model, $attribute, $items = [], $options = []) { $value = Html::getAttributeValue($model, $attribute); $field = $this->field($model, $attribute); $options['options'] = array_column($items, 'options'); $options['prompt'] = ''; return $this->staticOnly ? $field : $field->dropDownList(array_column($items, 'label', 'value'), $options); }In this updated version, we pass the $options array directly to the
dropDownList
method and use array_column
to extract the label-value pairs from the $items array