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

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

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.

  1. <?= $form->customDropDown($dpForm, 'color', [
  2. 'items' =>
  3. [
  4. 'label' => 'red',
  5. 'value' => 'red',
  6. 'options' => [
  7. 'style' => 'color: red'
  8. ]
  9. ],
  10. [
  11. 'label' => 'blue',
  12. 'value' => 'blue',
  13. 'options' => [
  14. 'style' => 'color: blue'
  15. ]
  16. ]
  17. ]

And the function I made looks like this (it uses and is located in ActiveForm):

  1. public function customDropdown($model, $attribute, $items = [], $options = [])
  2. {
  3. $value = Html::getAttributeValue($model, $attribute);
  4. $field = $this->field($model, $attribute, $options);
  5. return $this->staticOnly ? $field : $field->dropDownList($items);
  6. }

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:

  1. <optgroup label='0'>
  2. <option value="label">red</option>
  3. <option value="value">red</option>
  4. </optgroup>
  5. <optgroup label="options">
  6. <option value="style">color: red</option>
  7. </optgroup>

And so on. What I want looks like this:

  1. <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.

  1. <?= $form->customDropDown($dpForm, 'color', [
  2. 'items' =>
  3. [
  4. 'label' => 'red',
  5. 'value' => 'red',
  6. 'options' => [
  7. 'style' => 'color: red'
  8. ]
  9. ]
  10. [
  11. 'label' => 'blue',
  12. 'value' => 'blue',
  13. 'options' => [
  14. 'style' => 'color: blue'
  15. ]
  16. ]
  17. ]

And the function I made looks like this (it uses and is located in ActiveForm):

  1. public function customDropdown($model, $attribute, $items = [], $options = [])
  2. {
  3. $value = Html::getAttributeValue($model, $attribute);
  4. $field = $this->field($model, $attribute, $options);
  5. return $this->staticOnly ? $field : $field->dropDownList($items);
  6. }

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:

  1. <optgroup label='0'>
  2. <option value="label">red</option>
  3. <option value="value">red</option>
  4. </optgroup>
  5. <optgroup label="options">
  6. <option value="style">color: red</option>
  7. </optgroup>

And so on. What I want looks like this:

  1. <option value="red" style="color: red">red</option>

But I can't seem to get it.

答案1

得分: 1

为了实现所需的输出,其中下拉菜单中的每个项目都由一个单独的<option>标签表示,并具有指定的标签、值和样式,您需要按照以下方式修改您的代码:

在您的视图文件中,更新customDropDown函数调用以正确传递项目数组:

  1. <?= $form->customDropDown($dpForm, 'color', [
  2. [
  3. 'label' => 'red',
  4. 'value' => 'red',
  5. 'options' => [
  6. 'style' => 'color: red'
  7. ]
  8. ],
  9. [
  10. 'label' => 'blue',
  11. 'value' => 'blue',
  12. 'options' => [
  13. 'style' => 'color: blue'
  14. ]
  15. ],
  16. ]
  17. ); ?>

更新的方法:

  1. public function customDropdown($model, $attribute, $items = [], $options = [])
  2. {
  3. $value = Html::getAttributeValue($model, $attribute);
  4. $field = $this->field($model, $attribute);
  5. $options['options'] = array_column($items, 'options');
  6. $options['prompt'] = '';
  7. return $this->staticOnly ? $field : $field->dropDownList(array_column($items, 'label', 'value'), $options);
  8. }

在这个更新的版本中,我们直接将$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:

  1. &lt;?= $form-&gt;customDropDown($dpForm, &#39;color&#39;, [
  2. [
  3. &#39;label&#39; =&gt; &#39;red&#39;,
  4. &#39;value&#39; =&gt; &#39;red&#39;,
  5. &#39;options&#39; =&gt; [
  6. &#39;style&#39; =&gt; &#39;color: red&#39;
  7. ]
  8. ],
  9. [
  10. &#39;label&#39; =&gt; &#39;blue&#39;,
  11. &#39;value&#39; =&gt; &#39;blue&#39;,
  12. &#39;options&#39; =&gt; [
  13. &#39;style&#39; =&gt; &#39;color: blue&#39;
  14. ]
  15. ],
  16. ]
  17. ); ?&gt;

Updated method:

  1. public function customDropdown($model, $attribute, $items = [], $options = [])
  2. {
  3. $value = Html::getAttributeValue($model, $attribute);
  4. $field = $this-&gt;field($model, $attribute);
  5. $options[&#39;options&#39;] = array_column($items, &#39;options&#39;);
  6. $options[&#39;prompt&#39;] = &#39;&#39;;
  7. return $this-&gt;staticOnly ? $field : $field-&gt;dropDownList(array_column($items, &#39;label&#39;, &#39;value&#39;), $options);
  8. }

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:

确定