Rectangle 27 0

jquery ui Twitter Bootstrap: How to create a dropdown button with an accordion inside it?


.dropdown-accordion .panel-heading {
  padding: 0;
}
.dropdown-accordion .panel-heading a {
  display: block;
  padding: 10px 15px;
}
.find('.panel-collapse').collapse('hide')
.find('.panel-collapse.in').collapse('hide')
// Collapse accordion every time dropdown is shown
$('.dropdown-accordion').on('show.bs.dropdown', function (event) {
  var accordion = $(this).find($(this).data('accordion'));
  accordion.find('.panel-collapse.in').collapse('hide');
});

// Prevent dropdown to be closed when we click on an accordion link
$('.dropdown-accordion').on('click', 'a[data-toggle="collapse"]', function (event) {
  event.preventDefault();
  event.stopPropagation();
  $($(this).data('parent')).find('.panel-collapse.in').collapse('hide');
  $($(this).attr('href')).collapse('show');
})
<div class="dropdown dropdown-accordion" data-accordion="#accordion">
  <a data-toggle="dropdown" href="#" class="btn btn-primary">Dropdown trigger <span class="caret"></span></a>
  <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
    <li>
      <div class="panel-group" id="accordion">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">
              <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                Unit 1
              </a>
            </h4>
          </div>
          <div id="collapseOne" class="panel-collapse collapse">
            <div class="panel-body">
              <a href="#">Item a</a><br>
              <a href="#">Item b</a>
            </div>
          </div>
        </div>
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">
              <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                Unit 2
              </a>
            </h4>
          </div>
          <div id="collapseTwo" class="panel-collapse collapse">
            <div class="panel-body">
              <a href="#">Item a</a><br>
              <a href="#">Item b</a><br>
              <a href="#">Item c</a>
            </div>
          </div>
        </div>
      </div>
    </li>
  </ul>
</div>

You can insert an accordion inside a dropdown using :

You'll need to add a few lines of JS to prevent Dropdown events to break Collapse ones.

when the accordion opens for the first time - it is fully expanded. after reopening it again - it is in collapsed mode. how to prevent the accordoin from expanding on the first open?

Note