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

Functional interceptor causing Router.navigate() to malfunction

This is a custom Angular interceptor designed to handle potential session timeouts during the usage of an application. export const sessionExpiredInterceptor: HttpInterceptorFn = (req, next) => { const router: Router = inject(Router); return nex ...

Dynamic status bar for WordPress using Advanced Custom Fields

I am currently working on developing an order tracking system using Wordpress, ACF, and a form plugin that is yet to be determined for the user interface. While the back-end functionality is working smoothly, my focus now shifts towards creating a visual ...

Firefox having trouble displaying styles correctly

I just finished creating a website for my band, but I am having issues with it displaying correctly in Firefox. While everything looks great in Chrome and Safari, it seems like the stylesheet isn't loading at all in Firefox, leaving the page looking u ...

Enhancing WordPress Icons for Parent and Child Elements

I am seeking to customize icons on a WordPress website. The site contains a variety of product categories, including subcategories. My goal is to display one icon for parent categories and a different icon for child categories. I have tried the following ...

What could be causing my React components to not display my CSS styling properly?

If you're developing a React application and integrating CSS for components, ensure that you have included the style-loader and css-loader in your webpack configuration as shown below: module.exports = { mode: 'development', entry: &apo ...

Obtain the result and retrieve it using `window.angularComponentRef.zone.run`

In my work, I have been able to successfully call angular functions within a component through a static file. For reference, you can visit this link: How to expose angular 2 methods publicly?. The method suggested in the link involves using Zones in Angul ...

Interact with the horizontal click and drag scroller to navigate through various sections effortlessly, each designed to assist you

Currently, I am facing an issue with my horizontal scrolling feature in JavaScript. It works perfectly for one specific section that has a particular classname, but it fails to replicate the same effects for other sections that share the same classname. ...

Unable to modify the hover color on the image or icon

After creating a circle icon with an image in the center, I wanted to make the image change colors on hover. The main focus of the icon is the circle itself. In my attempt to achieve this effect, I included the following code for the circle icon: .circle- ...

What causes getBoundingClientRect() in Javascript to occasionally produce decimal values?

Currently, I am experimenting with an HTML5 canvas element. A major concern of mine is setting up a mousemove event to monitor the movement of the mouse over the canvas for drawing and other purposes. Unfortunately, I have not been able to locate a definit ...

Using the Angular Material Tree to showcase the hierarchical layout of an organization's

I am looking to visually represent the structure of an organization using an angular material tree, with properties such as position, salary, and years of service for each employee. class Employee { name: string; position: string; salary: number; ...

Is it possible for Azure static web apps to utilize Azure App Service as their backend?

Is it possible to have a Azure static web app communicate with an existing Azure app service in order for users to download a file generated by the app service? ...

Utilize switchMap to sequence calls

My goal is to execute rest requests sequentially using switchMap(...) from RxJs. Here is the object: export class Transaction { constructor( public id: string, public unique_id: string, public name: string, public status: string, pu ...

jQuery not displaying Div elements as expected

After experimenting with the Coverflow technique and adapting it to work with a div, I have encountered a slight issue. Below is the HTML code I am using: <html> <head> <meta http-equiv="Content-type" content="text/html; ...

Issues encountered while setting up @angular/google-maps on angular13

Every time I attempt to install, this error displays: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="402d21306d212323 ...

Reusing Form HTML Elements Based on Conditions

Here's a scenario where depending on the value of the $mode variable, either 'page' or 'popup' will be returned by a conditional. This choice will determine which HTML markup is displayed. Both scenarios contain a form element wit ...

I'm puzzled by the inconsistency of my scrolltop function on mobile, as it seems to be scrolling to various parts of the page depending on my

I am encountering an issue with a function that scrolls to a specific element when a button is clicked. Strangely, this functionality works fine on desktop but fails on mobile devices. It successfully scrolls to the correct position only when at the top of ...

Sideways movement of a single line within a table

Would you please help with a challenge I'm facing? I want to create a horizontally scrollable row that spans 100% of the page width without overflowing on top of the background image and while keeping the header text in focus. This is what I have att ...

Utilizing Data Filters to Embed HTML in Vue.js?

I have been attempting to utilize the Filter feature in Vue.js to insert HTML tags within a String. The documentation indicates that this should be achievable, however, I am not having any success. The objective is for the data to be just a String that is ...

What is the best way to animate the scaling of a CSS property using jQuery?

I need help creating an animation where a circle div with the class of "bubble" grows from nothing to its full size when a button is clicked using jQuery. I am currently facing difficulties in making it work properly. Here's my current code snippet: ...

Top technique for verifying the presence of duplicates within an array of objects

How can I efficiently check for duplicates in typescript within a large array of objects and return true or false based on the results? let testArray: { id: number, name: string }[] = [ { "id": 0, "name": "name1" }, ...