Bootstrap 4: How to create a dropdown menu with an accordion inside it?

安稳与你 提交于 2019-12-24 04:06:22

问题


I am actually facing some problems to get an bootstrap accordion working inside a dropdown menu. I googled a lot and also find some StackOverflow threads here, but they are several years old and depending on bootstrap 3 or bootstrap 2. Like this one here: Twitter Bootstrap: How to create a dropdown button with an accordion inside it?

Can anybody help me here or have a solution for a working accordion inside a bootstrap dropdown menu.

  <nav class="navbar navbar-expand-md">
            <ul class="navbar-nav">
                <li class="nav-item mega-dropdown-menu">
                    <a class="nav-link" href="#" id="dropdown01"
                       data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Test</a>
                    <div class="dropdown-menu" aria-labelledby="dropdown01">
                        <nav class="navbar">
                            <ul class="navbar-nav">
                                <li class="nav-item">
                                    <a class="nav-link" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">Accordion</a>
                                    <div id="collapseExample" class="collapse">
                                        <ul class="navbar-nav">
                                            <li class="nav-link">
                                                <a class="nav-link" href="#">
                                                    Accordion Link 1
                                                </a>
                                            </li>
                                            <li class="nav-link">
                                                <a class="nav-link" href="#">
                                                    Accordion Link 2
                                                </a>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                            </ul>
                        </nav>
                    </div>
                </li>
            </ul>
        </nav>

回答1:


The trick to keeping the Navbar dropdown open on click is to use a <form> tag. Therefore if you place the accordion inside a <form> it will work as desired.

  <div class="collapse navbar-collapse" id="navbarContent">
        <ul class="navbar-nav">
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Dropdown
                    </a>
                <div class="dropdown-menu">
                    <form class="accordion" id="accordionExample">
                        <div>
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                Item #1
                            </a>
                            <div id="collapseOne" class="collapse px-3 show" data-parent="#accordionExample">
                                Collapse 1 content
                            </div>
                        </div>
                        <div>
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                Item #2
                            </a>
                            <div id="collapseTwo" class="collapse px-3" data-parent="#accordionExample">
                                Collapse 2 content...
                            </div>
                        </div>
                        <div>
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                                Item #3
                            </a>
                            <div id="collapseThree" class="collapse px-3" data-parent="#accordionExample">
                                Collapse 3 content...
                            </div>
                        </div>
                    </form>
                </div>
            </li>
        </ul>
    </div>

Demo: https://www.codeply.com/go/F8iQy0MeLa




回答2:


try this

// Prevent dropdown to be closed when we click on an accordion link
$('.dropdown-accordion').on('click', 'a[data-toggle="collapse"]', function (event) {
  $($(this).attr('href')).collapse('toggle');
  event.stopPropagation();
});
<script src="http://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">                                        
  <div class="dropdown dropdown-accordion">
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
      Dropdown
    </button>
 
    <div id="accordion" class="dropdown-menu">
    <div class="card dropdown-item">
      <div class="card-header">
        <a class="card-link" data-toggle="collapse" href="#collapseOne">
           Item #1
        </a>
      </div>
      <div id="collapseOne" class="collapse show" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet
        </div>
      </div>
    </div>
    <div class="card dropdown-item">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseTwo">
        Item #2
      </a>
      </div>
      <div id="collapseTwo" class="collapse" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet
        </div>
      </div>
    </div>
    <div class="card dropdown-item">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseThree">
           Item #3
        </a>
      </div>
      <div id="collapseThree" class="collapse" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet
        </div>
      </div>
    </div>
  </div>
  </div>
</div>


来源:https://stackoverflow.com/questions/54416875/bootstrap-4-how-to-create-a-dropdown-menu-with-an-accordion-inside-it

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