英文:
Yii 2 custom dropdown menu with items not working
问题
I'm creating my own dropdownlist function in Yii 2. I created a function and a view, and in the view, I have multiple items inside of my dropdown form.
<?= $form->customDropDown($dpForm, 'color', [
'items' =>
[
'label' => 'red',
'value' => 'red',
'options' => [
'style' => 'color: red'
]
],
[
'label' => 'blue',
'value' => 'blue',
'options' => [
'style' => 'color: blue'
]
]
]
And the function I made looks like this (it uses and is located in ActiveForm):
public function customDropdown($model, $attribute, $items = [], $options = [])
{
$value = Html::getAttributeValue($model, $attribute);
$field = $this->field($model, $attribute, $options);
return $this->staticOnly ? $field : $field->dropDownList($items);
}
The problem is that when I open my dropdown, everything is either an option or an optgroup instead of just options with a label and a style.
How it looks like in Inspector:
<optgroup label='0'>
<option value="label">red</option>
<option value="value">red</option>
</optgroup>
<optgroup label="options">
<option value="style">color: red</option>
</optgroup>
And so on. What I want looks like this:
<option value="red" style="color: red">red</option>
But I can't seem to get it.
英文:
I'm creating my own dropdownlist function in Yii 2. I created a function and a view and in the view I have multiple items inside of my dropdown form.
<?= $form->customDropDown($dpForm, 'color', [
'items' =>
[
'label' => 'red',
'value' => 'red',
'options' => [
'style' => 'color: red'
]
]
[
'label' => 'blue',
'value' => 'blue',
'options' => [
'style' => 'color: blue'
]
]
]
And the function I made looks like this (it uses and is located in ActiveForm):
public function customDropdown($model, $attribute, $items = [], $options = [])
{
$value = Html::getAttributeValue($model, $attribute);
$field = $this->field($model, $attribute, $options);
return $this->staticOnly ? $field : $field->dropDownList($items);
}
The problem is that when I open my dropdown, everything is either an option or an optgroup instead of just options with a label and a style.
How it looks like in Inspector:
<optgroup label='0'>
<option value="label">red</option>
<option value="value">red</option>
</optgroup>
<optgroup label="options">
<option value="style">color: red</option>
</optgroup>
And so on. What I want looks like this:
<option value="red" style="color: red">red</option>
But I can't seem to get it.
答案1
得分: 1
为了实现所需的输出,其中下拉菜单中的每个项目都由一个单独的<option>
标签表示,并具有指定的标签、值和样式,您需要按照以下方式修改您的代码:
在您的视图文件中,更新customDropDown
函数调用以正确传递项目数组:
<?= $form->customDropDown($dpForm, 'color', [
[
'label' => 'red',
'value' => 'red',
'options' => [
'style' => 'color: red'
]
],
[
'label' => 'blue',
'value' => 'blue',
'options' => [
'style' => 'color: blue'
]
],
]
); ?>
更新的方法:
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);
}
在这个更新的版本中,我们直接将$options
数组传递给dropDownList
方法,并使用array_column
从$items
数组中提取标签-值对。
英文:
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 pass the items array correctly:
<?= $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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论