Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multilevel Dropdown in Bootstrap 3.3.5

I have the following HTML code and I've structured it to how I think it should be achieved based on the mark-up in the Bootstrap docs.

<div id="navbar" class="navbar-collapse collapse">
  <ul class="nav navbar-nav">
    <li class="active">
      <a href="#">Home</a>
    </li>

    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Hospital<span class="caret"></span></a>

      <ul class="dropdown-menu">
        <li class="dropdown">
          <a href="#">Consultants <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Adult cardiac surgery</a></li>
            <li><a href="#">Bariatric surgery</a></li>
            <li><a href="#">Colorectal surgery</a></li>
            <li><a href="#">Endocrine and thyroid surgery</a></li>
            <li><a href="#">Head and neck cancer surgery</a></li>
            <li><a href="#">Interventional cardiology</a></li>
            <li><a href="#">Lung Cancer</a></li>
            <li><a href="#">Neurosurgery</a></li>
            <li><a href="#">Orthopaedic surgery</a></li>
            <li><a href="#">Upper gastro-intestinal surgery</a></li>
            <li><a href="#">Urological surgery</a></li>
            <li><a href="#">Vascular surgery</a></li>
          </ul>
        </li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li role="separator" class="divider"></li>
        <li class="dropdown-header">Nav header</li>
        <li><a href="#">Separated link</a></li>
        <li><a href="#">One more separated link</a></li>
      </ul>
    </li>
  </ul>
</div>

However, this isn't working, it gives me this:

dropdown

When I click on the Consultants, it simply gets rid of the dropdown. Can multilevel drop-downs be achieved with Bootstrap CSS/JS alone? Or must we write custom CSS for it?

Any easy solutions that would work similar to how a single nest level work, and are responsive and touch-friendly (for tablets and phones)?

Thanks

like image 521
J86 Avatar asked Sep 16 '15 10:09

J86


1 Answers

I don't recall any default example from bootstrap 3 having 3 level drop down nav menu, but you can do like this, little bit of customization and java-script and it's fully responsive.

HTML

<div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li class="active"> <a href="#">Home</a>
        </li>
        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Hospital<b class="caret"></b></a>
            <ul class="dropdown-menu">
                <li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Consultants</a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Adult cardiac surgery</a>
                        </li>
                        <li><a href="#">Bariatric surgery</a>
                        </li>
                        <li><a href="#">Colorectal surgery</a>
                        </li>
                        <li><a href="#">Endocrine and thyroid surgery</a>
                        </li>
                        <li><a href="#">Head and neck cancer surgery</a>
                        </li>
                        <li><a href="#">Interventional cardiology</a>
                        </li>
                        <li><a href="#">Lung Cancer</a>
                        </li>
                        <li><a href="#">Neurosurgery</a>
                        </li>
                        <li><a href="#">Orthopaedic surgery</a>
                        </li>
                        <li><a href="#">Upper gastro-intestinal surgery</a>
                        </li>
                        <li><a href="#">Urological surgery</a>
                        </li>
                        <li><a href="#">Vascular surgery</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Another action</a>
                </li>
                <li><a href="#">Something else here</a>
                </li>
                <li role="separator" class="divider"></li>
                <li class="dropdown-header">Nav header</li>
                <li><a href="#">Separated link</a>
                </li>
                <li><a href="#">One more separated link</a>
                </li>
            </ul>
        </li>
    </ul>
</div>

CSS

.dropdown-submenu {
    position:relative;
}
.dropdown-submenu>.dropdown-menu {
    top:0;
    left:100%;
    margin-top:-6px;
    margin-left:-1px;
    -webkit-border-radius:0 6px 6px 6px;
    -moz-border-radius:0 6px 6px 6px;
    border-radius:0 6px 6px 6px;
}
.dropdown-submenu>a:after {
    display:block;
    content:" ";
    float:right;
    width:0;
    height:0;
    border-color:transparent;
    border-style:solid;
    border-width:5px 0 5px 5px;
    border-left-color:#cccccc;
    margin-top:5px;
    margin-right:-10px;
}
.dropdown-submenu:hover>a:after {
    border-left-color:#555;
}
.dropdown-submenu.pull-left {
    float:none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
    left:-100%;
    margin-left:10px;
    -webkit-border-radius:6px 0 6px 6px;
    -moz-border-radius:6px 0 6px 6px;
    border-radius:6px 0 6px 6px;
}

JS

(function ($) {
    $(document).ready(function () {
        $('ul.dropdown-menu [data-toggle=dropdown]').on('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            $(this).parent().siblings().removeClass('open');
            $(this).parent().toggleClass('open');
        });
    });
})(jQuery);

Working Example

like image 195
Shehary Avatar answered Oct 20 '22 16:10

Shehary