Yii 2自定义下拉菜单项未正常工作。

huangapple go评论73阅读模式
英文:

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 &lt;option&gt; 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:

&lt;?= $form-&gt;customDropDown($dpForm, &#39;color&#39;, [
        [
            &#39;label&#39; =&gt; &#39;red&#39;,
            &#39;value&#39; =&gt; &#39;red&#39;,
            &#39;options&#39; =&gt; [
                &#39;style&#39; =&gt; &#39;color: red&#39;
            ]
        ],
        [
            &#39;label&#39; =&gt; &#39;blue&#39;,
            &#39;value&#39; =&gt; &#39;blue&#39;,
            &#39;options&#39; =&gt; [
                &#39;style&#39; =&gt; &#39;color: blue&#39;
            ]
        ],
    ]
); ?&gt;

Updated method:

    public function customDropdown($model, $attribute, $items = [], $options = [])
{
    $value = Html::getAttributeValue($model, $attribute);

    $field = $this-&gt;field($model, $attribute);

    $options[&#39;options&#39;] = array_column($items, &#39;options&#39;);
    $options[&#39;prompt&#39;] = &#39;&#39;;

    return $this-&gt;staticOnly ? $field : $field-&gt;dropDownList(array_column($items, &#39;label&#39;, &#39;value&#39;), $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.

huangapple
  • 本文由 发表于 2023年6月29日 21:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定