"Sturdy Accordian Design: No Collapsing Issues with

Hello everyone, I'm attempting to recreate a collapsing accordion feature that I came across on https://getbootstrap.com/docs/4.1/components/collapse/. However, I'm facing issues as the accordions are not collapsing as expected. Strangely, they begin in the uncollapsed position, whereas in the example, they start off collapsed and then expand upon clicking a button. Here is my code – I hope someone can assist me with this problem.

Update: For those searching for solutions online, below is the code that worked for me. Take note that using data-target => attr.data-target is the only way to utilize text binding with data-target.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="accordion" id="accordionExample">
  <div class="card" *ngFor="let grocery of groceryList;index as index">
    <div class="card-header" id="grocery1{{index}}">
      <h5 class="mb-0">
        <button class="btn btn-link" type="button" data-toggle="collapse" attr.data-target="#grocery2{{index}}" aria-expanded="true" aria-controls="grocery2{{index}}">
          {{grocery.recipeName}}
        </button>
      </h5>
    </div>

    <div id="grocery2{{index}}" class="collapse show" aria-labelledby="grocery1{{index}}" data-parent="#accordionExample">
      <div class="card-body">
        <ul class="list-group" id="filterList">
          <li class="list-group-item">
            <a href="#" class="list-down-btn" data-toggle="#subgroup"><span class="glyphicon glyphicon-chevron-down"></span></a>
            <ul id="subgroup" class="list-group">
              <li class="list-group-item" *ngFor="let ingredient of grocery.ingredients">{{ingredient}}</li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>

Answer №1

Here is a breakdown of the issues you encountered:

  • The IDs are similar between headers and child elements
  • A double quote was missing
  • The simulation didn't work due to the "." in the ID causing breaks

PS: Based on your code, it appears there is a conflict between Bootstrap and another tool, resulting in the aria-controls not displaying properly.

Please see a working snippet demo below:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="accordion" id="accordionExample">
  <div class="card" *ngFor="let grocery of groceryList">
    <div class="card-header" id="grocery1">
      <h5 class="mb-0">
        <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#grocery2" aria-expanded="true" aria-controls="grocery2">
          {{grocery.recipeName}}
        </button>
      </h5>
    </div>

    <div id="grocery2" class="collapse show" aria-labelledby="grocery1" data-parent="#accordionExample">
      <div class="card-body">
        <ul class="list-group" id="filterList">
          <li class="list-group-item">
            <a href="#" class="list-down-btn" data-toggle="#subgroup"><span class="glyphicon glyphicon-chevron-down"></span></a>
            <ul id="subgroup" class="list-group">
              <li class="list-group-item" *ngFor="let ingredient of grocery.ingredients">{{ingredient}}</li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>

It seems that Angular and Bootstrap accordion/collapse functionality may have compatibility issues, as discussed in various Stack Overflow threads:

  • Bootstrap collapse menu doesn't work with Angular
  • Angular 4: Bootstrap's collapse does not work using data-target attribute
  • angular-bootstrap accordion won't open

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 best method for deleting scripts to optimize for mobile responsiveness?

My current plugin.js file houses all my plugins for responsive design, but it is unnecessarily large and cumbersome for mobile devices. I am considering creating two separate plugin.js files to toggle between for mobile and desktop views. What are the r ...

Angular checkbox filtering for tables

I have a table populated with data that I want to filter using checkboxes. Below is the HTML code for this component: <div><mat-checkbox [(ngModel)]="pending">Pending</mat-checkbox></div> <div><mat-checkbox [(ngModel ...

Guide on using jQuery to replace the applied css of an aspx control

My dilemma revolves around an AspxFileManager. I am attempting to distinguish a file when it is loaded by adding a class that places a border around the element. My initial attempt using .attr('style', 'border: 10px!important'); was uns ...

Is the "ngIf" directive not functioning properly within a "ngFor" loop in Angular?

I am in the process of developing an e-commerce website that focuses on selling a variety of shirts. Currently, I have set up a system where all the available shirts are displayed using an *ngFor loop. My goal is to create a feature that allows users to cl ...

Highlighting table rows using jQuery on click and restoring them on change by toggling between alternating

Hey there, I'm a newbie to jQuery and this is my first post. Spent some time searching but couldn't find exactly what I needed. I'm trying to have alternating row colors in a table; one class as NULL/empty and the other as alt. My jQuery sc ...

Transform Java code into HTML format for a visually appealing display similar to that in an Integrated Development Environment

Is there a way to showcase my Java code on an HTML page to resemble an Integrated Development Environment (IDE)? I've been searching for existing solutions but haven't found anything satisfactory yet. Ideally, I am looking for something similar t ...

Is it incorrect to append an array to a ul block using jQuery?

I am encountering an issue when trying to append an array to HTML. The array consists of HTML elements, starting with "li", then "img", and ending with "/li". When I try to append the array, it returns: <li></li> <img src="..."> Howeve ...

Masonry List with Next.js Image Fill

I am encountering an issue where the normal HTML img tag works perfectly fine inside my Masonry List. However, when I use the Next.js Image Component with a layout of fill, it fails to render anything. Custom Styles: const ImageList = styled('ul&apos ...

Troubleshooting the Issue with WordPress Image Customization Settings

I'm in the process of designing a Wordpress theme, but I've hit a roadblock. I'm utilizing the theme customize page to upload both a main logo and a mobile logo, which should then be displayed on the page. Initially, it was working for about ...

The value attribute in the HTML input tag being dynamically increased by JavaScript

Hi there, can someone help me figure out how to save changes to the value attribute of an HTML input tag that is being incremented by JavaScript? Currently, every time I click on a specific element, the input field should increase by one. The problem is th ...

Attempting to utilize the v-on:blur event to hide a context menu, but unfortunately, it is not functioning as expected

Working on a simple context menu in vue.js, I managed to successfully open it upon right-clicking a specific element using @contextmenu.prevent. However, my challenge lies in making the menu disappear when clicking outside of it. Despite trying v-on:blur a ...

Tips for customizing the ui-select element's stylesheet in Angular

I created a small program in Angular using a ui-select component. The issue I am facing is that the search bar is too long and I'm struggling to figure out how to shorten it. Typically, you can adjust the width properties in CSS to make changes like t ...

Troubleshooting the malfunctioning AngularJS ui-view component

My ui-view isn't functioning properly, but no errors are being displayed. Can anyone help me figure out what I'm missing to make this work? This is the main template, index.html. <!DOCTYPE html> <html> <head> <meta charset= ...

Adjust the width of the graphics on both sides of the text based on the length of the text content

Is there a way to center an H1 tag between two graphics using CSS or jquery/javascript? The width of the H1 text will vary depending on the page. The dot on the left should remain at the edge of the site, and the line should extend to meet the edge of the ...

Detecting a failed Promise with an Observable in Angular 6

I've been struggling to write unit tests for error handling in an Observable that wraps a Promise. The error doesn't seem to get caught as expected. I'm creating an Observable from a Promise that should reject, and then I return it from a ...

Use an input of map<string,string> when passing to an angular component

Trying to pass an input value for a component reuse, but facing the challenge of having to use a "hardcoded" map of strings. Uncertain about how to effectively pass this information: <continue-p [additionalInfo]="{ "myString": "str ...

My Jquery "animate" is only triggered once instead of on each function call. What could be causing this issue?

I have set my navigation to dock at a specific document height on the top of the viewport, and when it docks, it should display a dropdown animation. $(document).scroll(function(){ var x = $(window).scrollTop(); if(x>=700){ $("header ...

Is there a way to effectively transmit an observable array containing instances of Map<number, Employee> using the async pipe mechanism?

This is my Interface and Type Definition export interface EmployeeDetails { id: number; name: string; } export type EmployeesDirectory = Map<number, EmployeeDetails>; This is my Service Implementation class EmployeeServiceManager { employeesDa ...

Difficulty arising while trying to access the following element after the designated target using JQuery

JSFiddle I'm encountering a strange issue in the fiddle linked above. When typing a character into an input followed by pressing the down key, the console logs two values: one correct value of 3, and one incorrect value of 0. The challenge is to sel ...

configure the environment variable NODE_ENV in a NodeJS application

I am trying to properly set process.env.NODE_ENV to 'production' in my local NestJS code. Here are the steps I have attempted: Added NODE_ENV=production to the serve script in package.json Added nx build --prod to the build script in package.jso ...