Does bootstrap 3 collapse ('hide') open all collapsips?

I'm trying to achieve something fairly simple, but I can’t think it over - Google didn’t help, and the Bootstrap docs look a bit confusing.

What I need:

  • simple folding accordion with all the collapses closed when loading the page
  • All collapses can be closed with the click of a button.

Problem:

  • my solution only works when the user has opened / closed one or more collages.
  • when I call collapse('hide') , all collapsips open if they have not been opened manually before.

See Jsfiddle .

My markup:

 <a class="btn btn-default" id="collapseall"><span class="icon"></span>Collapse 'm all!</a> <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"> Collapsible Group Item #1 </a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse"> <div class="panel-body"> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </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"> Collapsible Group Item #2 </a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse"> <div class="panel-body"> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree"> Collapsible Group Item #3 </a> </h4> </div> <div id="collapseThree" class="panel-collapse collapse"> <div class="panel-body"> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> </div> 

Javascript:

 $('#collapseall').click(function () { $('.panel-collapse').collapse('hide'); return false; }); 

What am I missing here?

+6
source share
2 answers

I think you should only hide open ones like this.

 $('#collapseall').click(function(){ $('.panel-collapse.in') .collapse('hide'); }); 

Here is a working example of open / close: http://bootply.com/123636

+8
source

I had the same problem, so I looked into bootstrap.js code and realized what was going on.

If you call the collapse function without initializing the collapse element as javascript, Bootstrap will automatically initialize it first. Take a look at part of the new Collapse . You can understand why an already open item is not a problem here.

 // COLLAPSE PLUGIN DEFINITION // ========================== function Plugin(option) { return this.each(function () { var $this = $(this) var data = $this.data('bs.collapse') var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option) if (!data && options.toggle && option == 'show') options.toggle = false if (!data) $this.data('bs.collapse', (data = new Collapse(this, options))) if (typeof option == 'string') data[option]() }) } var old = $.fn.collapse $.fn.collapse = Plugin 

This is a collapse function:

 var Collapse = function (element, options) { this.$element = $(element) this.options = $.extend({}, Collapse.DEFAULTS, options) this.$trigger = $(this.options.trigger).filter('[href="#' + element.id + '"], [data-target="#' + element.id + '"]') this.transitioning = null if (this.options.parent) { this.$parent = this.getParent() } else { this.addAriaAndCollapsedClass(this.$element, this.$trigger) } if (this.options.toggle) this.toggle() } 

The last line is a period. If the toggle option is true, the toggle function is called. It opens a hidden folding element or closes the shown folding element:

 Collapse.prototype.toggle = function () { this[this.$element.hasClass('in') ? 'hide' : 'show']() } 

The toggle parameter defaults to true.

So, if you want to hide an element as shown below,

 $('.panel-collapse').collapse('hide'); 

you need to initialize the element as follows:

 $('#myCollapsible').collapse({ toggle: false }); 

You can find more information in Using crashes at boot .

0
source

All Articles