Optgroup in .NET Listbox

I need to group items in a Listbox similar to OPTGROUP into an html SELECT.

Any suggestions are welcome.

+2
source share
4 answers

Repaired Sandeep answer:

http://jsfiddle.net/kgBr9/

HTML

<select id="mySelect"> <option optGroup='a'>1</option> <option optGroup='a'>1</option> <option optGroup='a'>1</option> <option optGroup='b'>2</option> <option optGroup='b'>2</option> </select> 

JQuery

 function SetupOptGroups(select) { var optGroups=new Array(); var i = 0; $(select).find("[optGroup]").each(function(index, domEle) { var optGroup = $(this).attr("optGroup"); if ($.inArray(optGroup, optGroups)==-1) optGroups[i++] = optGroup; }); for(i=0; i < optGroups.length; i++){ $("option[optGroup='"+optGroups[i]+"']").wrapAll("<optgroup label='"+optGroups[i]+"'>"); } } 
+3
source

I met this requirement by adding an attribute to the parameters and the value of the attribute will be the name Optgroup that I want to have. And on the client side, I used this code to render a drop-down list using optgroups var optGroup = "";

 var i = 0; $(this).find("option").each(function () { if (optGroup !== "") optGroup += "," + $(this).attr("OptGroup"); else optGroup = $(this).attr("OptGroup"); }); var optGroups = $.unique(optGroup.split(",")); for (var optGroupEle in optGroups) { if ($("optgroup[label='" + optGroups[optGroupEle] + "']").html() == null) $("option[OptGroup='" + optGroups[optGroupEle] + "']").wrapAll("<optgroup label='" + optGroups[optGroupEle] + "'/>"); } 
+2
source

You can add an element and disable it, change the color, the final efect will be the same, i.e.

itemVersion.Attributes.Add ("disabled", "true") itemVersion.Attributes.Add ("style", "color: #CCCCCC") ddlAspectoOrigen.Items.Add (itemVersion)

0
source

You can use the HTML SELECT control and access all the properties of this from code, such as a list. You need to add a shortcut runat="server" .

0
source

Source: https://habr.com/ru/post/1384774/


All Articles