Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 material design <mat-expansion-panel> expand all / collapse all

I wanted to implement expand all and collapse all in angular 2 material. Can any one give idea? how to do that?

like image 259
Honey Avatar asked Mar 12 '18 08:03

Honey


People also ask

What does the <mat-expansion-panel> component do?

Sets the expanded state of the accordion item to true. Toggles the expanded state of the accordion item. This component corresponds to the header element of an <mat-expansion-panel>. Height of the header while the panel is collapsed. Height of the header while the panel is expanded.

How do I expand and collapse an expansion-panel?

Each expansion-panel must include a header and may optionally include an action bar. The <mat-expansion-panel-header> shows a summary of the panel content and acts as the control for expanding and collapsing.

What is afterexpand event in angular?

afterExpand: Emit the event whenever the panel is expanded. closed: Emits an event when the panel is closed. destroyed: Emits an event whenever the panel is destroyed. opened: Emits an event whenever the panel is opened. After doing all this now, we can check the steps required to follow to have the angular project setup from scratch.

What is the difference between a collapsed panel and expansion panel?

Upon selection, a collapsed panel expands, allowing users to add or edit information. Helper text may be added to the panel to assist the user. An expansion panel may use a focus state (by using a grey background on the list item) to provide focus to individual list items.


2 Answers

Source Link

For the latest version of Angular material 8

enter image description here

Template

<button mat-flat-button color="primary" (click)="openAllPanels()"><b>Open Panels</b></button> &nbsp;
<button mat-flat-button color="primary" (click)="closeAllPanels()"><b>Close Panels</b></button>

<mat-accordion [multi]="true"
#accordion="matAccordion"
>
  <mat-expansion-panel 
  #mapanel="matExpansionPanel"
  >
    <mat-expansion-panel-header>
        <b>Title</b>
    </mat-expansion-panel-header>
    <p>Description</p>
    <mat-action-row>
        <button mat-flat-button (click)="mapanel.close()">Click to close</button>
    </mat-action-row>
  </mat-expansion-panel>
</mat-accordion>

Component

import { MatAccordion } from '@angular/material';

...
...

@ViewChild('accordion',{static:true}) Accordion: MatAccordion


...

...

closeAllPanels(){
    this.Accordion.closeAll();
}
openAllPanels(){
    this.Accordion.openAll();
}


...
...  
like image 78
Code Spy Avatar answered Oct 04 '22 12:10

Code Spy


1- You should remove the mat-accordion to enable multiple expanded panels.

2- Use the expanded parameter to change multiple states at the same time.

Here is a running example.

EDIT

From version 6.0.0-beta-0 you can use multi parameter and the openAll and closeAll functions :

1- Change the mat-accordion element to set the muti to true and get the MatAccordionComponent instance :

<mat-accordion #accordion="matAccordion" [multi]="true">

2- Then use the openAll and closeAll functions to open or close all panels :

<button (click)="accordion.openAll()">Expand All </button>
<button (click)="accordion.closeAll()">Collapse All </button>

Here is a running example.

like image 29
ibenjelloun Avatar answered Oct 04 '22 12:10

ibenjelloun