Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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');
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...