Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sub Option Groups in Bootstrap-Select

The select code below is possible without using Bootstrap-Select plugin, but not when using it:

<div class="container">
    <select>
        <optgroup label="Group 1">
            <optgroup label="Sub Group 1">
                <option>Item 1</option>
                <option>Item 2</option>
                <option>Item 3</option>
            </optgroup>
            <option>Item 2</option>
            <option>Item 3</option>
        </optgroup>
        <optgroup label="Group 2">
            <option>Item 1</option>
            <option>Item 2</option>
            <option>Item 3</option>
        </optgroup>
    </select>
</diV>​

How can I use subgroups (nested optiongroups) using this plugin?

The first option group is not shown if I use nested. Only the second (children) optgroup and its options.

Here is the plugin website: http://silviomoreto.github.io/bootstrap-select/

like image 803
kamieduard Avatar asked Oct 12 '13 18:10

kamieduard


1 Answers

If your only reason for using Bootstrap Select is to have a 'prettier' select box you might consider using the custom-select class that is part of Bootstrap 4.x.

<select class="custom-select">
  <optgroup label="Group 1">
    <optgroup label="Sub Group 1">
      <option>Item 1</option>
      <option>Item 2</option>
      <option>Item 3</option>
    </optgroup>
  
    <option>Item 2</option>
    <option>Item 3</option>
  </optgroup>

  <optgroup label="Group 2">
    <option>Item 1</option>
    <option>Item 2</option>
    <option>Item 3</option>
  </optgroup>
</select>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

That being said, in this case the behavior you are seeing without the 3rd party plugin contradicts the guidelines of the element. The <optgroup> element is not supposed to be nested

like image 76
Robert Avatar answered Sep 28 '22 07:09

Robert