更改列表框中所选属性的优先级

时间:2015-07-22 23:36:59

标签: html listbox yii2

以下是示例代码:

IN CONTROLLER

 foreach($UserTypesArray as $key => $value)
    {
      $model->allowed_users[$key] = ['selected' => 'selected'];
    }
    $model->htmlOptions = array('multiple' => 'true', 'options' =>  $model->allowed_users);

视图

$form->field($model, 'user_type_id[]')->listBox($model->all_users ,  $model->htmlOptions);

好的,我们走了!

我已经搜索过“如何使用数据库中预先选择的值制作列表框”

发生了两件事

1)我最终编写了上面的代码(没有任何问题没有错误,完美运行)

2)我发现在htmlOptions中selected="selected"的优先级较低,因此会被覆盖并自动从所有option代码中删除

如果我将['selected' => 'selected']替换为['disabled' => 'disabled']它可以工作,并且所有option标记在列表框中被禁用,则更多...因为disabled具有更高的优先级所以它被推入option标记,但selected属性因为优先级较低而没有被推送。

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

There is a bug in yii itself I found on one of the yii forums. So I would like to share with you guys.

If you write it like that

['selected' => 'selected'];

It wont work.

But If you write it like that

['selected ' => 'selected']; or true

consider the space after selected. ['selected ' If you just put a space after selected it works perfectly.

Don't know why this is. But it works.