Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle mat-expansion-panel with button click?

Is there any way in which I can expand a particular mat-expansion-panel by clicking an external button?

I have tried linking to the ID of the panel, but with no success...

<mat-expansion-panel id="panel1"> ... </>
...
<button (click)="document.getElementById('panel1').toggle()>Click me</button>

Here is my stackblitz code for example

My eventual plan is to use this method to open different panels within a list generated from an array: <mat-expansion-panel *ngFor="let d of data"> ...

like image 710
physicsboy Avatar asked Feb 12 '18 11:02

physicsboy


People also ask

How do I disable expansion panel?

Disabling a panel Expansion panels can be disabled using the disabled attribute. A disabled expansion panel can't be toggled by the user, but can still be manipulated programmatically.


4 Answers

In your html file:

<mat-expansion-panel [expanded]="panelOpenState">

    <mat-expansion-panel-header>
        <mat-panel-title>
            TITLE
        </mat-panel-title>
    </mat-expansion-panel-header>

    <p>BODY</p>
</mat-expansion-panel>

<button mat-raised-button (click)="togglePanel">TOGGLE</button>

In your TS file:

panelOpenState: boolean = false;

togglePanel() {
    this.panelOpenState = !this.panelOpenState
}

If you use *ngFor to generate the expansion panels:

<mat-expansion-panel [expanded]="isOpen" *ngFor="let d of data">
    <mat-expansion-panel-header>
        {{ d.header }}
    </mat-expansion-panel-header>
    <p>{{ d.content }}</p>
</mat-expansion-panel>

<button mat-raised-button (click)="togglePanel">TOGGLE</button>

If you press the button all of the expanded panels opens simultaneously.

To open only one panel with one button, add a "expanded" property to the data array for each element like this:

  data = [
    {id:1, header:'HEADER 1', content:'CONTENT 1', expanded: false},
    {id:2, header:'HEADER 2', content:'CONTENT 2', expanded: false},
    {id:3, header:'HEADER 3', content:'CONTENT 3', expanded: false},
    {id:4, header:'HEADER 4', content:'CONTENT 4', expanded: false},
  ]

Then in your template:

<mat-expansion-panel [(ngModel)]="d.expanded" 
    [expanded]="d.expanded" *ngFor="let d of data" ngDefaultControl>

    <mat-expansion-panel-header>
        <button (click)="toggle(d.expanded)">TOGGLE</button>
        {{ d.header }}
    </mat-expansion-panel-header>
    <p>{{ d.content }}</p>

</mat-expansion-panel>

And the method raised by the button click:

  toggle(expanded) {
    expanded = !expanded;
  }
like image 158
Juan Avatar answered Oct 02 '22 14:10

Juan


<mat-expansion-panel [disabled]="true"
                     #mep="matExpansionPanel"
                     *ngFor="let foo of list">
  <mat-expansion-panel-header>
      <button (click)="mep.expanded = !mep.expanded">Toggle</button>
  </mat-expansion-panel-header>

  <p>Text</p>

</mat-expansion-panel>
like image 31
bodpad Avatar answered Oct 02 '22 14:10

bodpad


Use two-way binding on the expanded attribute of mat-expansion-panel. Here is an example live in StackBlitz:

https://stackblitz.com/edit/angular-gtsqg8

<button (click)='xpandStatus=xpandStatus?false:true'>Toggle it</button>
<p>
<mat-expansion-panel [(expanded)]="xpandStatus">
  <mat-expansion-panel-header>
    <mat-panel-title>This an expansion panel</mat-panel-title>
    <mat-panel-description>xpandStatus is {{xpandStatus}}</mat-panel-description>
  </mat-expansion-panel-header>
  Two-way binding on the expanded attribute gives us a way to store and manipulate the expansion status.
</mat-expansion-panel>
</p>
like image 5
Carl Sorenson Avatar answered Oct 02 '22 12:10

Carl Sorenson


You can use the method toggle().

First give the element an id.

<mat-expansion-panel #matExpansionPanel>

Next, access the element from javascript. Import necessary libraries (MatExpansionPanel, ViewChild)

@ViewChild(MatExpansionPanel, {static: true}) matExpansionPanelElement: MatExpansionPanel;

Lastly, call the toggle function

this.matExpansionPanelElement.toggle(); //open(), close()
like image 1
Sem Avatar answered Oct 02 '22 14:10

Sem