Expanding all cards in a Bootstrap collapse when one is clicked

Hello, I am currently working on integrating bootstrap collapse into a section of my django application.

Here is the code snippet that I have implemented so far:

{% extends 'base.html' %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-6">
            <div class="card">

                <h1 class="text-center">Recent reports</h1>

                <div class="accordion" id="accordionExample">

                    {% for report in reports %}

                    <div class="card">

                        <div class="card-header" id="report_{{ loop.counter }}">

                            <h2 class="mb-0">

                                {% if loop.counter == 1 %}

                                <button class="btn btn-link" type="button" data-toggle="collapse"
                                    data-target="#collapse{{ loop.counter }}" aria-expanded="true"
                                    aria-controls="collapse{{ loop.counter }}">

                                    Report #{{ report.report_number }}

                                </button>

                                {% else %}

                                <button class="btn btn-link collapsed" type="button" data-toggle="collapse"
                                    data-target="#collapse{{ loop.counter }}" aria-expanded="false"
                                    aria-controls="collapse{{ loop.counter }}">

                                    Report #{{ report.report_number }}

                                </button>

                                {% endif %}

                            </h2>

                        </div>

                        <div id="collapse{{ loop.counter }}" class="collapse"
                            aria-labelledby="report_{{ loop.counter }}" data-parent="#accordionExample">

                            <div class="card-body">

                                <table class="table">
                                    <tbody>
                                        <tr>
                                            <th scope="row">Date</th>
                                            <td>{{ report.datetime_submitted }}</td>
                                        </tr>
                                        <tr>
                                            <th scope="row">XYZ</th>
                                            <td>{{ report.xyz }}</td>
                                        </tr>
                                        .....
                                    </tbody>
                                </table>

                            </div>

                        </div>

                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

I'm running into an issue where clicking on the card-header card causes all the collapse sections with the card-body class to expand instead of just the associated one (as intended).

Any tips or guidance on how to resolve this would be greatly appreciated.

Answer №1

Learning curve error: I managed to fix the issue by utilizing {{ forloop.counter }} as opposed to {{ loop.counter }}.

Answer №2

Opt for {{loop.index}} over {{loop.counter}}.

Answer №3

When your cards are placed inside a div with the display:flex property, it results in all card-body classes expanding and collapsing simultaneously. #uniquefeature

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the maximum number of JavaScript functions allowed in a single HTML file?

My HTML5 file includes JavaScript (created from CoffeeScript) directly within the HTML file, as I prefer this approach for my HTML/JavaScript programming. The code consists of four JavaScript functions that convert one temperature unit to another. Strang ...

How to modify an array in an HTML document using BeautifulSoup

Running a Python script, I am trying to access a local HTML file containing a JavaScript array inside a script tag that needs updating. Here is the test code snippet: from bs4 import BeautifulSoup html = ''' <script> var myArray ...

Excessive content spilling over the div container

Hello everyone I've been trying to set up a div that includes an image, a title, and some text. I want the image to be aligned on the left side, with the title and text following on the right. However, I'm having an issue where the text is overf ...

Transmitting data for a PHP variable within Wordpress.org

I am a newbie when it comes to PHP programming. Currently, I am working on a WordPress web page where selecting an option from a drop-down menu and clicking a button labeled "Consultar" in a form should display the queried data in a table below. However, ...

Having trouble with Google Font Api not showing up on the server, but it works fine

I'm attempting to incorporate a Google Font into my website. I have included the font in the head section of my html code like so: <link href='http://fonts.googleapis.com/css?family=Monsieur+La+Doulaise' rel="stylesheet" type="text/css"& ...

motion graphic border not joining at the corner

Yesterday, my code was functioning perfectly. However, after making several revisions to include all animations and styles, I notice that the animated borders no longer connect at three corners. Can someone please assist in identifying what went wrong duri ...

Managing Positioning of PHP Print Statement

Recently, I developed a PHP script for my site that facilitates user registration and login specifically for membership purposes. If a user successfully logs in, the script redirects them to a landing page for members. However, if the login fails, the scri ...

Revamp your Redux Form with a fresh new 'Field' label style

Is there a way to style the label of a Redux Form field? The Redux Form API does not provide information on how to apply styles to the label. Despite applying the classes.formField class, the label itself remains unstyled. Could this be an issue with inhe ...

Field cannot be submitted without the required input after an Ajax request

I want to ensure that all my text/email input forms require a submission before you can "Submit" the email. Unfortunately, when using Ajax to prevent the page from refreshing after pressing the button, the required attribute does not function properly. T ...

The input field must only accept numbers with decimal points

I have developed a dynamic HTML table that includes various input controls. I am looking to restrict input to only decimal values with two digits after the point. For example: 1111.22 , 44445454.33 Here is the HTML code snippet: <div id="divdynam ...

What are the steps to ensure that the content within the <pre><code></code></pre> tags displays correctly in Internet Explorer 7?

When using JQuery to make an AJAX pull with JSON to an ASP.net Web Service, the following code is used: $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "TestWebService.asmx/Foo", data: "{}", dataType: "json", success: function( ...

Can anyone tell me the technique for verifying whether DEBUG is set to true or false in a Django template,

Is there a way to distinguish the appearance of a toolbar in layout.html based on whether DEBUG is set to True or False? I found a solution that involves using django.core.context_processors.debug, but it requires me to use RequestContext instead of Reque ...

Element that hovers and floats

Just last week, I was working on coding a custom menu bar for my blog located at . However, when it came to adding social buttons, things went awry. It seemed like a float element issue! Despite my attempts to resolve it by adjusting widths and properties, ...

Updating every occurrence of a string in the visible text of a webpage: A step-by-step guide

I need to include the registered trademark symbol beside all mentions of a brand, which I'll refer to as "SomeBrand", on a client's website. Here is my current approach: function updateSomeBrand(){ var elements = document.getElementsByTagName( ...

Employing the "div" element to target in CSS styling

I'm dealing with a document structure that is consistent across multiple pages. HTML: <ul> <li> <div> <img src="" /> </div> </li> </ul> Presently, there is a border aroun ...

developing a tri-step program to assist the user

Is there a way to create responsive circles with equal width and height, center the horizontal line, and also center the numbers in the middle of the circle? .DCQ_Container { display: flex; flex-direction: column; text-align: center; } .Progress_ ...

Is it possible to utilize window.location.replace function within an iframe?

Is it possible to use window.location.replace to bypass history and target on-page anchors without page reloads, but encounter issues when working within iframes? The main problem seems to be a CSP (content security policy) violation for script-src ' ...

PHP: Sending multiple values under a single name

Here's the code I'm currently working with. It includes a form with 6 inputs. The last 3 inputs are named firstanswer, secondanswer, and thirdanswer. <form action="addsurvey.php" method="post"> <input type="text" name="surveyname"> ...

Issues with alignment arise when adjusting the font size of labels using radio buttons in Bootstrap

Currently, I am dealing with an issue related to the alignment of radio buttons and their labels in the bootstrap style. The problem becomes apparent when I adjust the font size of the label: the radio button remains the same size in its original position ...

Modifying Bootstrap Grid Layouts

I need to reorganize the columns for better responsiveness as the current order is not ideal. Below is the code snippet: <div class="tab-content" id="pills-tabContent"> <div class="tab-pane fade show active" id="pills-home" role="tabpanel" ari ...