How to close all the menus inside an Accordion in Angular 4

丶灬走出姿态 提交于 2020-01-15 14:47:28

问题


I am new to angular 4 here I have an accordion in the structure of Category,Group and Subgroup.

If I clicked on category it shows all the groups in it and if I clicked on the groups it shows all the subgroups .

Now everything is working fine .

What I want to do is If I clicked on the Category again I want to close the accordion with all the groups which are opened before.

In my case if I opened some groups and closed the accordion then If I opened again it shows the groups opened which I have opened before .

I have reproduced my code in stackblitz : https://stackblitz.com/edit/angular-bootstrap-carousel-dynamic2-ghebrf?file=app/app.component.html

can anyone help me to fix it .


回答1:


i don't believe it's possible to achieve what you are looking for just by using href and ids, because href takes only one argument. Check here.

I added event bindings to check when the parent accordion is being toggled to close all the child accordions too.

Added a click event here:

    <a class="accordion-toggle h6" data-toggle="collapse" data-parent="#accordion1" (click)="onToggle()"  href="#collapseTwo + i">
        {{data?.CAMD_ENTITY_DESC}}
    </a>

and used a ngIf here:

<div [id]="'collapseInnerTwo' + j"  *ngIf="accordion2==true" class="accordion-body collapse" style="margin-left:10px;margin-top:3px">

Check the full solution here: https://stackblitz.com/edit/angular-bootstrap-carousel-dynamic2-fjowve?file=app/app.component.html



来源:https://stackoverflow.com/questions/51281799/how-to-close-all-the-menus-inside-an-accordion-in-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!