Bootstrap Accordion navigation with dropdown icon-chevron

北战南征 提交于 2019-12-04 09:48:07

It seems that bootstrap collapse plugin not fully implemented for accordions. It toggles collapsed css class only for clicked element.

 $(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) {
    var $this = $(this), href
      , target = $this.attr('data-target')
        || e.preventDefault()
        || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
      , option = $(target).data('collapse') ? 'toggle' : $this.data()

    // this line
    $this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed')

    $(target).collapse(option)
  })

UPD To fix this you just need to find all toggle buttons and do the same with them:

$(document).on('click.collapse.data-api', '.accordion-toggle', function(event) {
    var $this = $(this),
        parent = $this.data('parent'),
        $parent = parent && $(parent);

    if ($parent) {
        $parent.find('[data-toggle=collapse][data-parent=' + parent + ']').not($this).addClass('collapsed');
    }
});

And don't forget to add accordion-group classes.

http://jsfiddle.net/NBcmh/29/

If you're using Font Awesome 3.2.1, then in the css, use the following to call the changes:
.collapsed .icon-chevron:before {content: "\f078";} .icon-chevron:before {content: "\f077";}

Thank you sody. Here is a fix which did not require changing HTML markup. Add the following js prior to var target:

    $(parent).find('.accordion-toggle').not($(event.target)).addClass('collapsed');

Credit due to a developer on this project.

http://jsfiddle.net/utcwebdev/NBcmh/24/

Is it possible to add another level to the accordion?

<li>
    <a href="#li02" data-toggle='collapse' data-target='#subnav01', data-parent='#sidenav01' class="accordion-toggle collapsed">
        <i class="icon-chevron-up pull-right"></i>Page 2 </a>
      <ul id="subnav01" class="nav nav-list collapse">
        <li><a href="#1">Page 2.1</a></li>
        <li><a href="#" data-toggle='collapse' data-target='#subnav01.1', data-parent='#sidenav01.1' class="accordion-toggle collapsed">
            <i class="icon-chevron-up pull-right"></i>Page 2.2</a>
            <ul id="subnav01.1" class="nav nav-list collapse">
                <li><a href="#1">Sub-level 1</a></li>
                <li><a href="#2">Sub-level 2</a></li>
            </ul>
        </li>
        <li><a href="#6">Page 2.3</a></li>
      </ul>
</li>

I added this portion of html but it's not showing it.

http://jsfiddle.net/labanino/G3PqR/12/

I was stuck on a similar problem, however I have managed to find the solution for the accordion arrows to switch but I can't figure how to keep the active one always open while being able to collapse the others.

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