更改列表框中所选属性的优先级 [英] change the priority of selected attribute in listbox

查看:75
本文介绍了更改列表框中所选属性的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是示例代码:

在控制器中

 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);

好,我们开始!

我在如何使用数据库中的预选值制作列表框"方面进行了很多搜索

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

发生了两件事

1)我最终编写了上面的代码(没有问题,没有错误,可以正常工作)

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

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

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

如果我将['selected' => 'selected']替换为['disabled' => 'disabled'],它会起作用,并且所有option标记在列表框中都将被禁用....由于disabled具有更高的优先级,因此将其推入option标记,但selected属性由于优先级低而无法推送.

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 ?

推荐答案

我在yii论坛之一中发现yii本身有一个错误.所以我想和你们分享.

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

如果您这样写

['selected' => 'selected'];

它行不通.

但是如果您这样写

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

请在选择后考虑空格. ['selected '如果在选择后仅放一个空格,则效果很好.

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.

这篇关于更改列表框中所选属性的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆