What causes the entire set of dynamically created cards to collapse when using the toggle collapse button in ngb-bootstrap's Collapse control?

I am currently implementing the ngb-bootstrap collapse feature in my Angular 9 application.

Incorporating the ngb-bootstrap collapse functionality within a Bootstrap card, I have multiple cards being generated. Each card is equipped with a collapse button in the header section that is linked to the corresponding body of that specific card. This design ensures that only the targeted card collapses upon button click, preventing the entire set of cards from collapsing simultaneously. Despite correctly setting the aria-controls attribute with the id of the ngbCollapse div as illustrated in the code snippet below, I encounter an issue where clicking the button results in collapse of the entire card set.

<div class="animated fadeIn" style="margin-left: 2%; margin-right: 2%; margin-top: 2%;" id="qgroup-div-g{{ei}}" *ngFor="let g of this.s?.MyQuestionGroup; let ei=index;">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <div class="row" style="position: relative;">
                        <div class="col-6 col-sm-4 col-md-2 col-xl mb-3 mb-xl-0">      
                            <h5 style="text-align: left;">
                                {{g?.QuestionGroupName}} 
                                <button type="button" 
                                        class="btn btn-success mr-1" 
                                        style="position: absolute; right: 10px; top: 5px;"  
                                        (click)="isCollapsed = !isCollapsed"
                                        [attr.aria-expanded]="!isCollapsed" 
                                        attr.aria-controls="qgroup-collapse-wrapper-g{{ei}}">
                                    <i class="fa fa-align-justify"></i>
                                </button>
                            </h5>             
                        </div>
                    </div>    
                </div>

                <div [ngbCollapse]="isCollapsed" id="qgroup-collapse-wrapper-g{{ei}}">
                    <div class="card-body">
                        <div class="table-responsive">
                            sample text 1
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Answer №1

The reason behind this issue is that the variable isCollapse is treating each card as an individual entity. To rectify this, you should change it to an array of boolean values to represent each card separately.

For instance:

this.MyQuestionGroup = [
  {
    QuestionGroupName: "GroupName1"
  },
        {
    QuestionGroupName: "GroupName2"
  },
        {
    QuestionGroupName: "GroupName3"
  }
];
for(let i=0; i<this.MyQuestionGroup.length; i++){
  this.isCollapsed.push(false);
}

Then in your HTML:

(click)="isCollapsed[ei] = !isCollapsed[ei]"
                            [attr.aria-expanded]="!isCollapsed[ei]" 


        <div [ngbCollapse]="isCollapsed[ei]">

        </div>

You can also view a functional example on stackblitz by visiting collapse card

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

Responsive CSS - reordering div elements

#div1 { position: relative; max-width: 100%; height: 100px; background-color: green; color: white; } #div2 { position: relative; max-width: 100%; height: 100px; background- ...

Can we find a superior method?

I am currently using a stateful service to retrieve questions. The service checks if the questions exist in the local state, and if they do, it returns them. Otherwise, it makes an HTTP call to the API to fetch and store the questions. The service method ...

A unique string containing the object element "this.variable" found in a separate file

Snippet of HTML code: <input class="jscolor" id="color-picker"> <div id="rect" class="rect"></div> <script src="jscolor.js"></script> <script src="skrypt.js"></script> Javascript snippet: function up ...

Issue detected in the ngx-joyride package: Error found in ./node_modules/ngx-joyride/assets/images/close.svg

During my build process, I encountered an error with ngx-joyride: ERROR in ./node_modules/ngx-joyride/assets/images/close.svg Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type." <line x1="1" y1=" ...

Transitioning from MVC to Angular 2 and TypeScript: A Step-by-Step Guide

Seeking guidance as I venture into the world of Angular. My goal is to pass a string variable 'element' to my UI and utilize it. However, I am unsure about how to go about passing it inside main.js and beyond. How can I transfer a variable to a t ...

How can Javascript be used to interact with buttons and select options?

Can anyone help me with creating a JavaScript code that can select an option from a drop down, click on that option, and then add it to the cart by clicking another button? So far, I have attempted the following: browser.getelementbyid("id").invokemembe ...

How to display an image in a pop-up with a title

I am currently using the Bootstrap framework and I am trying to create a popup image with a title, but it only shows the image. I am not sure how to add code in JavaScript to display the title as well. Can someone please assist me? Here is my code: Javas ...

What is the best way to extract information from a JSON file and display it on a webpage using

I am new to this and I have a question for everyone Here's an example of the JSON response from my URL: The JSON data returned is as follows: { "Data":{ "id": 1312, "Name": "Steem Dollars", "Symbol": "SBD", "website_slug": "steem-dollars", "Level": ...

What is stopping this paragraph from being vertically centered with just one line?

After following instructions from another user, I successfully centered text vertically. However, I encountered difficulties when trying to center the entire content vertically. My attempt to enclose the text in its own div did not yield the desired result ...

Exploring WordPress's Query Page with Posts

My current code successfully pulls posts from a specified category, but I am struggling to also include pages in the output. I have tried various methods, but haven't been able to pull in both posts and pages at the same time. HTML/PHP <?php quer ...

Negative results encountered: Karma test on Chrome Headless failed due to an uncaught null exception

During the execution of unit tests, failures occur intermittently on different tests with no clear error message provided. ... Chrome 89.0.4389.114 (Linux x86_64): Executed 1225 of 1453 SUCCESS (0 secs / 3 mins 29.829 secs) Chrome 89.0.4389.114 (Linux x86_ ...

Tips on ensuring JSON object structure with type and interface in an Angular application

In my angular project, I have created a router config file to handle routing efficiently. This config file contains JSON data like this: router.config.ts { branch:{ list:'/app/branch/list', add:'/app/branch/add' }, ...

How can I use regex within a pipe to split a string in Angular 4?

I need to implement a method where I can split a string based on special characters and spaces in the given regex format, excluding "_". Example: #abc_xyz defgh // output #abc_xyz Example: #abc@xyz defgh // output #abc Example: #abc%xyz&defgh // out ...

How can you set an input field to be initially read-only and then allow editing upon clicking a button using Vue.js?

//I have two divs that need to be set as readonly initially. When an edit button is clicked, I want to remove the readonly attribute and make them editable. <div> <input type="text" placeholder="<a href="/cdn-cgi/l/email-protection ...

Encountering an issue with extending the MUI color palette, receiving a "reading 'dark'" error due to properties of undefined

Encountering an issue when trying to expand the MUI color palette—getting this error instead: Error: Cannot read properties of undefined (reading 'dark') Take a look at my theme.ts file below: const theme = createTheme({ palette: { pri ...

Ways to retrieve the user's IP address and provide the information in JSON format

Although I am not an expert in PHP, I specialize in developing Android apps. One of the challenges I face is extracting the user's IP address from a specific URL . This URL provides various information when accessed, but my main requirement is to retr ...

A step-by-step guide on coding a slider image

<?php $query1="SELECT * FROM user_photos_offline WHERE ssmid='$ssmid' AND status='1' ORDER BY date_uploaded DESC LIMIT 0,5"; $sql=mysql_query($query1); $count=mysql_num_rows($sql); ...

Issues arising when trying to generate HTML email content from a document

Looking to send an HTML email using Python, argparse, and the command line. I want to insert content from an HTML file directly into the email body rather than just attaching the file. So far, my attempts are only resulting in the HTML file being attache ...

Is anyone else experiencing issues with the jQuery slide-in not working on a particular website? How can I determine which version of jQuery is compatible with this site?

Essentially, I am looking to have a div box slide in on the page as it loads. This method has worked successfully on other websites and HTML previews that I have tested it on so far. However, for some reason, it does not seem to work on this specific websi ...

Creating an animated Gif using HTML and CSS styling

I am trying to create an effect where a .gif animation plays once when the mouse hovers over a specific image. I have one static image in PNG format and one animated gif. The goal is for the gif to play every time the mouse is hovered over the png image. ...