Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to close a mat-accordion when other mat-accordion is opened?

I'm using Angular Material with Angular v6. I have two accordions where each accordion contains an expansion panel. How can I close the opened accordion when I open the other accordion? Both of the accordions stay open now!

Code:

<!-- first accordion -->
<mat-accordion>
    <mat-expansion-panel mat-list-item *ngIf="menuitem.type === 'sub'">
      <mat-expansion-panel-header class="main-header">
        <mat-panel-title>
          <mat-icon class="list-icon">{{ menuitem.icon }}</mat-icon>
          <span class="name">{{ menuitem.name }}</span>
        </mat-panel-title>
      </mat-expansion-panel-header>

      <mat-nav-list *ngFor="let child of menuitem.children" routerLinkActive="open">
        <a class="grand-menu" mat-list-item href="javascript:;" *ngIf="child.type === 'x'" [routerLink]="['/', menuitem.state, child.state ]">
          <span class="grand">{{ child.name }}</span>
        </a>
      </mat-nav-list>

      <mat-accordion open>
        <mat-expansion-panel mat-list-item *ngFor="let childitem of menuitem.children" routerLinkActive="open" #example>
          <mat-expansion-panel-header class="child" *ngIf="childitem.grand_children">
            <mat-panel-title class="child-title">
              <mat-icon class="arrow-icon" *ngIf="!example.expanded">keyboard_arrow_right</mat-icon>
              <mat-icon *ngIf="example.expanded">keyboard_arrow_down</mat-icon>
              {{ childitem.name }}
            </mat-panel-title>
          </mat-expansion-panel-header>

          <mat-nav-list *ngFor="let child of childitem.grand_children" routerLinkActive="open">
            <a class="grand-menu" mat-list-item href="javascript:;" *ngIf="childitem.type === 'parent'" [routerLink]="['/', menuitem.state, childitem.state, child.state ]">
              <span class="grand">{{ child.name }}</span>
            </a>
          </mat-nav-list>
        </mat-expansion-panel>
      </mat-accordion>
    </mat-expansion-panel>
  </mat-accordion>

   <!-- second accordion -->
  <mat-accordion>
      <mat-expansion-panel mat-list-item *ngIf="menuitem.type === 'sub1'">
        <mat-expansion-panel-header class="main-header">
          <mat-panel-title>
            <mat-icon class="list-icon">{{ menuitem.icon }}</mat-icon>
            <span class="name">{{ menuitem.name }}</span>
          </mat-panel-title>
        </mat-expansion-panel-header>

        <mat-nav-list *ngFor="let child of menuitem.children" routerLinkActive="open">
          <a class="grand-menu" mat-list-item href="javascript:;" *ngIf="child.type === 'x'" [routerLink]="['/', menuitem.state, child.state ]">
            <span class="grand">{{ child.name }}</span>
          </a>
        </mat-nav-list>
      </mat-expansion-panel>
    </mat-accordion>
like image 322
fariba.j Avatar asked Nov 16 '25 05:11

fariba.j


1 Answers

You can use index while looping and integers while not looping the mat-expansion-panel.

<mat-accordion [togglePosition]="'before'" multi>
<mat-expansion-panel [expanded]="state === 0" (opened)="setState(0)">
    <mat-expansion-panel-header>
        <mat-panel-title>
            <p class="mt-4">Title 1</p>
        </mat-panel-title>
    </mat-expansion-panel-header>
    <app-upload></app-upload>
</mat-expansion-panel>
<br>
<mat-expansion-panel [expanded]="state === 1" (opened)="setState(1)">
    <mat-expansion-panel-header>
        <mat-panel-title>
            <p class="mt-4">Title 2</p>
        </mat-panel-title>
    </mat-expansion-panel-header>
    <app-upload></app-upload>
</mat-expansion-panel>

In ts file:

step: number = -1; // -1 makes all the accordians to be closed. Can replace 0 with -1 to open the required accordion open on page load.

setState(stateNumb: number) {
  this.state = stateNumb;
}
like image 66
Panchakshari Puranmatt Avatar answered Nov 17 '25 20:11

Panchakshari Puranmatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!