change the priority of selected attribute in listbox

我与影子孤独终老i 提交于 2019-12-25 17:55:08

问题


Following is the example code :

IN CONTROLLER

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

IN VIEW

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

OK here we go!

I have searched a lot on "how to make list box with pre selected values from the database"

Two things happened

1) I ended up writing the above code (which has no issue no error, works perfectly)

2) I found out that in htmlOptions the selected="selected" has a low priority so it it gets overwritten and is automatically removed from all option tags

further more if I replace ['selected' => 'selected'] with ['disabled' => 'disabled'] it works and all the option tags are disabled in the listbox .... As disabled has a higher priority so it gets pushed in the option tag but the selected attribute does not get pushed because of it low priority.

Any one know how to fix that ?


回答1:


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.



来源:https://stackoverflow.com/questions/31575808/change-the-priority-of-selected-attribute-in-listbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!