Bootstrap 3 collapse accordion: collapse all works but then cannot expand all while maintaining data-parent

后端 未结 4 461
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 00:52

I\'m using Bootstrap 3 and trying to setup the following accordion/collapse structure:

  1. Onload: Each accordion panel in a group is fully collapsed and functi

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 01:40

    For whatever reason $('.panel-collapse').collapse({'toggle': true, 'parent': '#accordion'}); only seems to work the first time and it only works to expand the collapsible. (I tried to start with a expanded collapsible and it wouldn't collapse.)

    It could just be something that runs once the first time you initialize collapse with those parameters.

    You will have more luck using the show and hide methods.

    Here is an example:

    $(function() {
    
      var $active = true;
    
      $('.panel-title > a').click(function(e) {
        e.preventDefault();
      });
    
      $('.collapse-init').on('click', function() {
        if(!$active) {
          $active = true;
          $('.panel-title > a').attr('data-toggle', 'collapse');
          $('.panel-collapse').collapse('hide');
          $(this).html('Click to disable accordion behavior');
        } else {
          $active = false;
          $('.panel-collapse').collapse('show');
          $('.panel-title > a').attr('data-toggle','');
          $(this).html('Click to enable accordion behavior');
        }
      });
    
    });
    

    http://bootply.com/98201

    Update

    Granted KyleMit seems to have a way better handle on this then me. I'm impressed with his answer and understanding.

    I don't understand what's going on or why the show seemed to be toggling in some places.

    But After messing around for a while.. Finally came with the following solution:

    $(function() {
      var transition = false;
      var $active = true;
    
      $('.panel-title > a').click(function(e) {
        e.preventDefault();
      });
    
      $('#accordion').on('show.bs.collapse',function(){
        if($active){
            $('#accordion .in').collapse('hide');
        }
      });
    
      $('#accordion').on('hidden.bs.collapse',function(){
        if(transition){
            transition = false;
            $('.panel-collapse').collapse('show');
        }
      });
    
      $('.collapse-init').on('click', function() {
        $('.collapse-init').prop('disabled','true');
        if(!$active) {
          $active = true;
          $('.panel-title > a').attr('data-toggle', 'collapse');
          $('.panel-collapse').collapse('hide');
          $(this).html('Click to disable accordion behavior');
        } else {
          $active = false;
          if($('.panel-collapse.in').length){
            transition = true;
            $('.panel-collapse.in').collapse('hide');       
          }
          else{
            $('.panel-collapse').collapse('show');
          }
          $('.panel-title > a').attr('data-toggle','');
          $(this).html('Click to enable accordion behavior');
        }
        setTimeout(function(){
            $('.collapse-init').prop('disabled','');
        },800);
      });
    });
    

    http://bootply.com/98239

提交回复
热议问题