简体   繁体   English

select2:有没有一种方法可以将类从选项传播到列表中显示的项目?

[英]select2: is there a way to propagate a class from an option to the item presented in the list?

In my scenario, I'm preloading a select with several options and I was wondering if there's any way to add a class to the li that get generated by the control so that I can change its styles. 在我的场景中,我正在预加载带有几个选项的select,我想知道是否有任何方法可以将类添加到由控件生成的li中,从而可以更改其样式。 Here's the code I'm using on the server side to generate the select (ASP.NET MVC): 这是我在服务器端用于生成选择(ASP.NET MVC)的代码:

<select id="areas" name="areas" class="form-control" multiple="multiple">
    @foreach (var area in Model.AreasAssociadas) {
        <option value="@area.IdArea" selected="selected" @Html.Raw(area.Active ? "" : "class='disabled'")>@area.Nome</option>
    }
</select>

Now, I'd like to change the default UI, but only for those items that are disabled (ie, for all the items where the Active property returns false). 现在,我想更改默认UI,但仅更改那些已禁用的项目(即,对于Active属性返回false的所有项目)。 Btw, I want to add a style so that I can get something like this: 顺便说一句,我想添加一种样式,以便获得如下内容:

在此处输入图片说明

Can this be done? 能做到吗?

Since it seems like there's not a property or event to handle this, I've ended up adding codes for changing the dom on the fly. 由于似乎没有属性或事件可以处理此问题,因此我最终添加了用于动态更改dom的代码。 not the best option, but gets the work done... 不是最好的选择,但是可以完成工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM