Search code examples
htmlangularfrontendngforcard

When I press header , all cards expand. I have fetch data from API and need to show in a card list


Here I have attached my code. What is the solution to just expand the card which I only pressing?

<div *ngFor="let edsrecord of listofEDS">
        <div class="available_data_sources_block" id="accordion">
          <div class="card">
            <div class="card-header" id="headingOne">
              <h5 class="mb-0">
                <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true"
                  aria-controls="collapseOne">
                  {{edsrecord.dataSources[0].ERPName.toString()}}
                </button>
              </h5>
            </div>

            <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
              <div class="card-body">
                Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf
                moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod.
                Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda
                shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea
                proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim
                aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
              </div>
            </div>
          </div>
        </div>
      </div>

Solution

  • You did not specify, but from the looks of the code I assume you use bootstrap to do this card action.

    Bootstrap assumes, that data-target gets the id of the card which should be opened/closed.

    In your example, every button and card has the same data-target/id combo. That is the cause of your error. In Order to fix this, every card need a unique id.

    This might sound easy, however I did not get it to work when dynamically assigning values to the properties.

    Here is an working example https://stackblitz.com/edit/angular-ivy-eqskk3

    First is used a workaround with ngClass.

    In a second attempt I did it with jQuery just like bootstrap would provided in the documentation https://getbootstrap.com/docs/4.6/components/collapse/#via-javascript

    If you want this functionality out of the box without so much trouble, i would suggest you take a look at https://material.angular.io/components/expansion/