2014-04-09 10 views
6

Ich versuche, etwas ziemlich einfaches zu erreichen, aber kann mich nicht darum kümmern - Google war keine Hilfe, und Bootstrap-Dokumente scheinen ein wenig verwirrend zu sein.bootstrap 3 collapse ('hide') öffnet alle Collapsible?

Was ich brauche:

  • einfach zusammenklappbar Akkordeon, mit allen Collapsibles in geschlossenem Zustand auf Seite Last
  • alle Collapsibles können mit einem Klick auf eine Schaltfläche geschlossen werden.

Das Problem:

  • meine Lösung funktioniert nur, wenn der Benutzer geöffnet hat/geschlossen eine oder mehrere Collapsibles
  • wenn ich collapse('hide') nennen, alle Collapsibles geöffnet sind, soweit sie nicht bereits gewesen war zuvor manuell geöffnet.

Siehe Jsfiddle.

Meine 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> 

Die Javascript: hier

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

Was bin ich?

Antwort

8

Ich glaube, Sie nur hide die offen diejenigen wie diese haben ..

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

Hier sind ein funktionierendes Öffnen/Schließen all Beispiele: http://bootply.com/123636

+1

Das ist in der Tat eine vernünftige Lösung, danke. Obwohl ich immer noch mit einem Gefühl zurückgeblieben bin, verstehe ich die Logik hinter den show/hide/toggle Parametern des bootstrap collapsible nicht ganz ... – ptriek

0

Ich hatte dasselbe Problem, so dass ich sah in Bootstrap .js Code und herausgefunden, was vor sich geht.

Wenn Sie die collapse-Funktion aufrufen, ohne das collapse-Element als Javascript zu initialisieren, initialisiert Bootstrap es automatisch zuerst. Schauen Sie sich die neue Collapse Teil. Sie können sehen, warum ein bereits geöffnetes Element hier kein Problem darstellt.

// 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 

Dies ist Minimierungsfunktion:

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() 
} 

Die letzte Zeile ist Punkt. Wenn die Toggle-Option wahr ist, wird Toggle Funktion aufgerufen. Es öffnet sich versteckt zusammenklappbar Element oder zusammenklappbare Element schließt gezeigt:

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

Die Toggle-Option ist standardmäßig.

Also, wenn Sie Element wie unten ausblenden möchten,

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

Sie benötigen Element wie folgt zu initialisieren:

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

Sie finden weitere Informationen in Bootstrap collapse usage finden.

Verwandte Themen