Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: Collapse other sections when one is expanded

I am making a Rails app, and am trying to achieve a particular functionality relating to Twitter's Bootstrap collapse. Bear with me as I explain it.

I currently have the following view:

enter image description here

When each of these buttons is clicked, their data-toggle div is expanded. The view is set-up as follows:

    <button class="btn dropdown" data-toggle="collapse" data-target="#keys"><i class="icon-chevron-right"></i> Keys <span class="badge badge-info pull-right"><%= @app.keys.count %></span></button>     <button class="btn dropdown" data-toggle="collapse" data-target="#attrs"><i class="icon-chevron-right"></i> Attributes</button>     <button class="btn dropdown" data-toggle="collapse" data-target="#edit"><i class="icon-chevron-right"></i> Edit Details</button>      <div class="collapse indent" id="keys">         <!--content-->     </div>      <div class="collapse indent" id="attrs">         <!--content-->     </div>      <div class="collapse" id="edit">         <!--content-->     </div> 

I have them set-up like this, because I want the buttons side by side, in a row. If I move the buttons to be right above the view they expand/collapse, then the buttons stack on top of one another.

So, my end goal is to have the three buttons side-by-side and have them collapse and expand their respective sections. The current set-up works, however is a little awkward. For example, if someone expands the keys section and then expands the attributes section, they have to scroll below the keys section.

There are two possible solutions to this problem. One is that pressing one button causes the other 2 to collapse themselves. This would mean that at any given time, only one of these sections is expanded.

The better solution, I think would be to have it so that when keys is expanded, the buttons to the right move down to the bottom of the keys div, and when attributes is expanded, the edit details button moves to the bottom of that div. Is this possible? I have already tried to do it by letting the buttons stack on top of each other and changing their relative locations through css, but that didn't work because when one of the sections was expanded, the other buttons ended up in awkward spots in the middle of the expanded section.

Lastly, I would like to try to do this without the accordion style behavior as mentioned on twitter's bootstrap page, but if someone can convince me from a design standpoint that it is preferable, I would certainly reconsider.

like image 915
finiteloop Avatar asked Jul 13 '12 18:07

finiteloop


People also ask

How do you close a collapsible DIV when opening a new one?

Before opening a collapsed div, your code will have to close all open divs. I left off the bit that does the collapsing, fixed now. See the working example. OK, it now allows manual closing of an open div.

How do you expand and collapse in Bootstrap?

To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element. Then add the data-target="#id" attribute to connect the button with the collapsible content (<div id="demo">).

How do you keep multiple collapses open in Bootstrap 4?

Assuming that you're using Bootstrap 4, you can simply remove the data-parent attribute from the element with the collapse class. This subscribes the collapse to events on #accordionExample , which is the main accordion element, via the data-parent attribute.

How do I close all accordion in Bootstrap?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.


2 Answers

Using data-parent, first solution is to stick to the example selector architecture

<div id="myGroup">     <button class="btn dropdown" data-toggle="collapse" data-target="#keys" data-parent="#myGroup"><i class="icon-chevron-right"></i> Keys  <span class="badge badge-info pull-right">X</span></button>     <button class="btn dropdown" data-toggle="collapse" data-target="#attrs" data-parent="#myGroup"><i class="icon-chevron-right"></i> Attributes</button>     <button class="btn dropdown" data-toggle="collapse" data-target="#edit" data-parent="#myGroup"><i class="icon-chevron-right"></i> Edit Details</button>      <div class="accordion-group">         <div class="collapse indent" id="keys">             keys         </div>          <div class="collapse indent" id="attrs">             attrs         </div>          <div class="collapse" id="edit">             edit         </div>     </div> </div> 

Demo (jsfiddle)

Second solution is to bind on the events and hide the other collapsible elements yourself.

var $myGroup = $('#myGroup'); $myGroup.on('show.bs.collapse','.collapse', function() {     $myGroup.find('.collapse.in').collapse('hide'); }); 

Demo (jsfiddle)

PS: the strange effect in the demos is caused by the min-height set for the example, just ignore that.


Edit: changed the JS event from show to show.bs.collapse as specified in Bootstrap documentation.

like image 184
Sherbrow Avatar answered Nov 23 '22 14:11

Sherbrow


If you don't want to change your markup, this function does the trick:

jQuery('button').click( function(e) {     jQuery('.collapse').collapse('hide'); }); 

Whenever a BUTTON is clicked, all sections become collapsed. Then bootstrap opens the one you selected.

like image 44
GrafiCode Avatar answered Nov 23 '22 12:11

GrafiCode