I am having trouble getting the collapsible panels to work within my foreach loop. When an item is clicked, all of the items expand/retract, which isn't what I want. I want each element to be independent.
I am connected to a database and basically want to display the data simply and be able to expand to see more information.
I've tried lots of different solutions, my current method is based off https://stackoverflow.com/a/18568997/1366033 What should I do about the id and href?
Any help would be greatly appreciated.
foreach (var item in groupItem){
<div class="panel-group" id="accordion">
<div class="panel panel-default" id="panel1">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-target="#collapseOne"
href="#collapseOne">
@Html.DisplayFor(modelItem => item.Name)
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">@Html.DisplayFor(modelItem => item.IdNumber)
</div>
</div>
</div>
</div>
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.
Always open Omit the data-bs-parent attribute on each .accordion-collapse to make accordion items stay open when another item is opened.
Expand or collapse accordion items on checkbox click in Angular Accordion component. By default, accordion items expand or collapse by clicking the accordion item header or clicking expand/collapse icon in accordion header. You can also expand or collapse the accordion items through external button click.
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">).
Basically you are creating panel with loop and assigning the same id
to all the panel-group
and that's what causing the problem here! So you can try working as below and please note ids
should be unique in DOM
@{int i=0;}
foreach (var item in groupItem)
{
<div class="panel-group" id="accordion_@i">
<div class="panel panel-default" id="panel_@i">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-target="#collapseOne_@i" href="#collapseOne_@i">
@Html.DisplayFor(modelItem => item.Name)
</a>
</h4>
</div>
<div id="collapseOne_@i" class="panel-collapse collapse in">
<div class="panel-body">
@Html.DisplayFor(modelItem => item.IdNumber)
</div>
</div>
</div>
</div>
i++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With