I have created a customer c# DropDownList control that can render out it's contents are optgroups (Not from scratch, I edited some code found on the internet, although I do understand exactly what it's doing), and it works fine.
However, I have now come across a situation where I need to have two levels of indentation in my dropdown, i.e.
<select>
<optgroup label="Level One">
<option> A.1 </option>
<optgroup label="Level Two">
<option> A.B.1 </option>
</optgroup>
<option> A.2 </option>
</optgroup>
</select>
However, in the example snippet above, it is rendering as if Level Two
was at the same amount of indentation as Level One
.
Is there a way to produce the nested optgroup behavior I am looking for?
I think if you have something that structured and complex, you might consider something other than a single drop-down box.
The HTML spec here is really broken. It should allow nested optgroups and recommend user agents render them as nested menus. Instead, only one optgroup level is allowed. However, they do have to say the following on the subject:
And user agents could start using submenus to render optgoups instead of displaying titles before the first option element in an optgroup as they do now.
Ok, if anyone ever reads this: the best option is to add four
s at each extra level of indentation, it would seem!so:
I needed clean and lightweight solution (so no jQuery and alike), which will look exactly like plain HTML, would also continue working when only plain HTML is preset (so javascript will only enhance it), and which will allow searching by starting letters (including national UTF-8 letters) if possible where it does not add extra weight. It also must work fast on very slow browsers (think rPi - so preferably no javascript executing after page load).
In firefox it uses CSS identing and thus allow searching by letters, and in other browsers it will use
prepending (but there it does not support quick search by letters). Anyway, I'm quite happy with results.You can try it in action here
It goes like this:
CSS:
HTML (class "i1", "i2" etc denote identation level):
JavaScript:
I know this was quite a while ago, however I have a little extra to add:
This is not possible in HTML5 or any previous specs, nor is it proposed in HTML5.1 yet. I have made a request to the
public-html-comments
mailing list, but we'll see if anything comes of it.Regardless, whilst this is not possible using
<select>
yet, you can achieve a similar effect with the following HTML, plus some CSS for prettiness:As an extra added benefit, this also means you can allow selection of the
<optgroups>
themselves. This might be useful if you had, for example, nested categories where the categories go into heavy detail and you want to allow users to select higher up in the hierarchy.This will all work without JavaScript, however you might wish to add some to hide the radio buttons and then change the background color of the selected item or something.
Bear in mind, this is far from a perfect solution, but if you absolutely need a nested select with reasonable cross-browser compatibility, this is probably as close as you're going to get.
I really like the Broken Arrow's solution above in this post. I have just improved/changed it a bit so that what was called labels can be toggled and are not considered options. I have used a small piece of jQuery, but this could be done without jQuery.
I have replaced intermediate labels (no leaf labels) with links, which call a function on click. This function is in charge of toggling the next div of the clicked link, so that it expands/collapses the options. This avoids the possibility of selecting an intermediate element in the hierarchy, which usually is something desired. Making a variant that allows to select intermediate elements should be easy.
This is the modified html:
A small javascript/jQuery function:
And the css:
Running sample in JSFiddle