Managing sections on Bootstrap 4: Automatically close open sections when the section above them is closed

A unique report was designed with a Bootstrap accordion feature to expand and collapse grouped data. The sections within the accordion include Monthly, Weekly, Daily, and Services.


        <thead style="font-size:12px">
            ...
        </thead>
        <tbody>
            @foreach (var server in Model.Report)
            {
                ...
                @foreach (var month in server.Monthly)
                {
                    ...
                    @foreach (var week in month.Weekly)
                    {
                        ...
                        @foreach (var day in week.Daily)
                        {
                            ...
                            @foreach (var service in day.Services)
                            {
                                ...
                            }
                        }
                    }
                }
            }
        </tbody>
    </table>

https://i.sstatic.net/zK3YF.png

The goal is to make it so that when any upper section is clicked closed, the lower open section will also close automatically. Currently, closing an upper section does not affect the lower open sections.

Here's what I have attempted

Attempts were made using data-parent and JQuery to address this issue, but no successful solution has been found yet.


        $(".collapse").on('show.bs.collapse', function () {
            $(this).collapse('hide');
            alert('Trigger');
        });
    });

Your guidance on how to progress would be greatly appreciated!

Answer №1

After conducting further investigation and coding, I pinpointed the issue in my approach. My javascript function was designed to switch plus signs to minus signs when a section is expanded.

function Update(val) {
        var ele = "p_" + val;
        var sign = document.getElementById(ele).innerHTML;
        if (sign == "+") {
            document.getElementById(ele).innerHTML = "-";
        } else {
            document.getElementById(ele).innerHTML = "+";
        }
    }

Because Bootstrap necessitates jQuery, I came up with this solution:

function Update(val) {
        var ele = "p_" + val;
        var sign = document.getElementById(ele).innerHTML;
        if (sign == "+") {
            document.getElementById(ele).innerHTML = "-";
        } else {
            document.getElementById(ele).innerHTML = "+";
            var subs = "AA_" + val;
            var list = $('*[id^="'+ subs +'"]')
            for (var i = 0; i < list.length; i++) {
                if ($(list[i]).is(":visible")) {
                    $(list[i]).collapse('toggle');
                }
                var base = "#p" + $(list[i]).attr("id").substring(2);
                if ($(base).text() == "-") {
                    $(base).text("+");
                }
            }
        }
    }

I anticipate some criticism for using both javascript and jQuery in the same function, but it serves its purpose perfectly. Now, clicking the minus sign on the Monthly section results in any Weekly, Daily, or Services sections closing, while reverting the minus sign of the open section back to a plus sign for the next interaction.

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

Ways to manually conceal a popover using Bootstrap 5

I am attempting to generate multiple Bootstrap popover instances that can all be closed with a single click. Here is the approach I took: document.addEventListener('DOMContentLoaded', function () { var items = []; document.getElementBy ...

Filtering the data in the table was successful, but upon searching again, nothing was found. [Using Angular, Pagination, and

Having a table with pagination, I created a function that filters the object and displays the result in the table. The issue arises when I perform a new search. The data from the initial search gets removed and cannot be found in subsequent searches. Bel ...

Keep Dropdown menu open when clicking on a menu item to avoid closing

I'm facing an issue with a drop-down menu in the sidebar that contains links as menu items. Whenever I click on a menu item (link), the link works fine but the drop-down menu closes. What I actually want is for the drop-down menu to stay open even af ...

Angular and JavaScript Performing Slide-Up Animation

Currently, I am working on creating a menu using Angular and JavaScript. My goal is to have this menu toggle between showing and hiding when a button is clicked. If you would like to view the code that I have written so far, you can check out the demo her ...

Tips for deploying a Vue.js website on IONOS?

I recently created a website using vue and found myself in need of uploading it online. Despite feeling unsure, I decided to go with IONOS since it was recommended by Google. However, I am currently facing challenges trying to upload my site as the proce ...

How to clear input fields in Ant Design ReactJS components

Utilizing the form list component from antd in my react.js project https://ant.design/components/form/#Form.List I have a basic form list set up where I can easily add or remove fields. At times, I need to reset the form and remove any additional items t ...

Callback function for Jeditable when the form is reset

My situation involves having a string with multiple lines separated by line breaks, which is then parsed into an <ul> hierarchy using JavaScript on the client side. The editing of this list is done using Jeditable, where the user sees the original te ...

What could be causing my PlaneGeometry to not cast a shadow?

I am currently utilizing three.js to develop a scene featuring a model. The scene includes a plane that the model is positioned on, as well as a spotlight illuminating the model. The model is comprised of various objects, all of which have been configured ...

A dynamic modal window built with ReactJS

I am struggling to understand how to make my app function properly. There is a component called ContactAdd that should render the component ModalWindow when clicked. The ModalWindow component requires a parameter called isOpened={this.state.open}. How c ...

Replacing an existing pie chart with a new one using JavaScript

I created pie charts using chartjs V2.6.0, and everything was working fine until I encountered an issue. Whenever I input new data into the same chart, it keeps displaying the previous data when hovering over. I attempted to fix this by using the $('# ...

Triggering an event upon completion of ng-repeat's execution

I am facing a challenge in updating the style of a specific element after ng-repeat has finished changing the DOM. The directive I have implemented for triggering ng-repeat works perfectly fine when adding items to the model, but it does not get called whe ...

Can an event be passed from one Vue.js component to another without relying on a global EventBus?

I have two independent components that are not directly related to each other. However, I want them to be able to communicate so that when an event is triggered in one component, the other component can respond accordingly. For example, let's conside ...

Angular's radio button is set to "checked" on a pre-configured model

Looking for help with customizing alignment of images in a bootstrap/angular template? Check out the code snippet below: <div ng-repeat="a in attributes"> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-white ...

Is it possible to enhance the presentation of numerous elements with JavaScript and jQuery?

I am currently in the process of developing an application utilizing the Google Maps APIs. Specifically, I have a list of postal codes for my country that I need to display on the map. You can see a demonstration of my current implementation in the video ...

Has the removal of Javascript conditional compilation been confirmed in IE11?

I have been using javascript conditional compilation to determine the version of the IE Trident engine: var ieVersion = undefined; /*@cc_on ieVersion = Math.floor(@_jscript_version); @*/ Despite working fine for IE8, 9, and 10, in IE11, the conditiona ...

Generate a text input field within a dropdown menu

Below is an example of my basic HTML code for a drop-down list: <span id="Div_List"> <label for="Gender">For:</label> <select name="Gender" id="Sex"> <option value="1">1000mtr</option> <option val ...

Using Slim Framework and AJAX to handle the forward slash character as a parameter

I am using an ajax call to communicate with a web service built on the Slim framework. This service is responsible for storing notes in my database. One issue I am facing is that users are allowed to input strings like "send 1/2 piece". This causes a prob ...

Folding into itself lies another folding

While attempting to nest a collapse area inside another collapse, I encountered an issue. Upon clicking to open the first collapse, it functions perfectly. However, when trying to open the second collapse (nested within the first one), it inadvertently cl ...

A guide to retrieving the contents of an input field based on its assigned value and name attributes through Javascript

Can anyone help me with selecting an input element in my HTML code? Here is the input I want to select: <input id="tb-radio-gym_custom_radio-10ce67e3" type="radio" value="OUI" name="form_fields[gym_custom_radio]" data-price-inc="0" checked="checked"> ...

Searching the Evernote API using Node.js to retrieve search results from two specific notebooks

I've noticed that Evernote's search syntax doesn't support searching in multiple notebooks at once. However, I still need to retrieve all notes containing 'somesearch' either in "notebook 1" or "notebook 2". Currently, I can perfo ...