2014-04-09 12 views
6

मैं कुछ आसान हासिल करने की कोशिश कर रहा हूं, लेकिन इसके चारों ओर मेरा सिर नहीं मिल सकता - Google को कोई मदद नहीं मिली है, और बूटस्ट्रैप दस्तावेज़ थोड़ा उलझन में प्रतीत होते हैं।बूटस्ट्रैप 3 पतन ('छुपाएं') सभी collapsibles खुलता है?

मैं क्या जरूरत है:

  • सरल सिमटने अकॉर्डियन, पृष्ठ लोड
  • सभी collapsibles एक बटन पर एक क्लिक के साथ बंद किया जा सकता पर बंद स्थिति में सभी collapsibles साथ।

समस्या:

  • मेरी समाधान केवल काम करता है जब उपयोगकर्ता खोल दिया है/बंद एक या अधिक collapsibles
  • जब मैं collapse('hide') फोन, सभी collapsibles खोल रहे हैं जब तक कि वे पहले से ही किया गया था मैन्युअल रूप से पहले खोला गया।

Jsfiddle देखें।

मेरे मार्कअप:

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

जावास्क्रिप्ट:

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

क्या मैं यहाँ याद आ रही है?

उत्तर

8

मुझे लगता है कि आप केवल hide इस तरह खुला लोगों के लिए है ..

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

यहाँ एक काम कर खोलने/बंद सभी उदाहरण है: http://bootply.com/123636

+1

यह वास्तव में एक उचित समाधान है, धन्यवाद। हालांकि मुझे अभी भी एक भावना के साथ छोड़ दिया गया है, लेकिन मैं बूटस्ट्रैप ढहने के शो/छुपा/टॉगल पैरामीटर के पीछे तर्क को पूरी तरह से समझ नहीं पा रहा हूं ... – ptriek

0

मैं एक ही समस्या थी, तो मैं बूटस्ट्रैप में देखा .js कोड, और पता लगाया कि क्या हो रहा है।

यदि आप संकुचित तत्व को जावास्क्रिप्ट के रूप में प्रारंभ किए बिना पतन समारोह कहते हैं, तो बूटस्ट्रैप स्वचालित रूप से इसे पहले प्रारंभ करता है। नया संकुचित भाग देखें। आप देख सकते हैं कि पहले से ही खोला तत्व क्यों एक समस्या नहीं है।

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

अंतिम पंक्ति है:

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

यह पतन कार्य है। यदि टॉगल विकल्प सही है, टॉगल फ़ंक्शन कहा जाता है। यह छिपा सिमटने तत्व खोलता है या सिमटने तत्व दिखाए बंद कर देता है:

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

टॉगल विकल्प को सही पर डिफ़ॉल्ट है।

इसलिए, यदि आप नीचे के रूप में तत्व को छिपाना चाहते हैं,

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

आप इस प्रकार का तत्व प्रारंभ करने की आवश्यकता:

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

आप Bootstrap collapse usage में अधिक जानकारी प्राप्त कर सकते हैं।

संबंधित मुद्दे