Search code examples
htmloptgroup

Nesting optgroups in a dropdownlist/select


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?


Solution

  • Ok, if anyone ever reads this: the best option is to add four &nbsp;s at each extra level of indentation, it would seem!

    so:

    <select>
     <optgroup label="Level One">
      <option> A.1 </option>
      <optgroup label="&nbsp;&nbsp;&nbsp;&nbsp;Level Two">
       <option>&nbsp;&nbsp;&nbsp;&nbsp; A.B.1 </option>
      </optgroup>
      <option> A.2 </option>
     </optgroup>
    </select>