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

孤街醉人 提交于 2019-12-18 04:50:37

问题


In Twitter Bootstrap, I am trying to create a button dropdown with an accordion inside it.

the purpose is to present a long list of items in a concise form (i.e. avoid long scroll-downs by the user).

the straightforward way of including an accordion div inside a dropdown div does not work..:

<div class="dropdown">
    <a data-toggle="dropdown" href="#">Dropdown trigger</a>
    <div class="dropdown-menu" role="menu" aria-labelledby="dLabel" id="accordion">
    <h3>Unit 1</h3>
    <div>
      <p>item a</p>
      <p>item b</p>
    </div>
    <h3>Unit 2</h3>
    <div>
      <p>item c</p>
      <p>item d</p>
      <p>item e</p>
    </div>
  </div>            
</div>

Any ideas how can one implement this?


回答1:


You can insert an accordion inside a dropdown using :

  • Bootstrap Dropdown component
  • Bootstrap Collapse component

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

Working example on Bootply

<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>
.dropdown-accordion .panel-heading {
  padding: 0;
}
.dropdown-accordion .panel-heading a {
  display: block;
  padding: 10px 15px;
}
// 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');
})


来源:https://stackoverflow.com/questions/20323656/twitter-bootstrap-how-to-create-a-dropdown-button-with-an-accordion-inside-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!