Bootstrap Collapse

Asked

Viewed 665 times

1

I’m creating a list using the collapse in an interaction of loop, but are all starting open, I would like only the first item of my collapse stay open.

If you double-click on one of the items, it closes all the others, leaving open only the one that was clicked.

My view

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
    @for (int i = 0; i < 10; i++)
    {
        <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="heading_@i">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapse_@i" aria-expanded="true" aria-controls="collapse_@i"
                       @{ if (i == 0) { <text> collapse</text>} else { <text></text>}}>
                        Collapsible Group Item #@i
                    </a>
                </h4>
            </div>
            <div id="collapse_@i" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading_@i">
                <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>

Edited at Luis' suggestion.

2 answers

1


the solution is to change the div:

<div id="collapse_@i" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading_@i">

for

<div id="collapse_@i" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading_@i">

removing the style in

the style in makes the element where it is used, visible, as can be seen in its definition which is found inside the file bootstrap.css

.collapse.in {
  display: block;
  visibility: visible;
}
  • @rman, I’m no expert on css, but I think I’ve made my point with what I’ve added to the response.

  • So, it’s more so the answer doesn’t get so "dry" anyway. Surely the explanation now helps (: - (I will delete my comments to not "dirty" the answer).

-1

I’m not sure where you define that the element should be Collapse, but put a (i == 0) ? 'collapse' : '';. It may not be the best solution, but it solves your problem.

  • Luis, you didn’t, I’ve updated my view code as you proposed.

  • Once you’ve discovered the problem, you have to remove the in of class of div before the panel-body.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.