Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make angular material expansion panel only open programatically and not when the panel header is clicked

I have an mat-expansion-panel, which opens when the mat-expansion-panel-header is clicked. Is there a way to disable this behavior? I want to open the expansion panel elsewhere using its open method, and disable the ability to open it by clicking the header.

like image 887
phelhe Avatar asked Oct 10 '18 14:10

phelhe


People also ask

How can I prevent a mat-expansion-panel from opening when clicked?

Prevent Clicking the toggle in perticular button in mat-expansion-panel-header. using this you can prevent clicking on perticular element (click)="! exppanel.

How to disable expansion panel in angular Material?

To disable the panel, add the disabled directive to the panel element and set it to true. To enabled it, set it to false. We can also group multiple panels by using the mat-accordion element.

How do I keep the mat accordion open by default?

It is a directive we can add on the mat-expansion-panel to hide icons up and down on the header section. If we set this attribute value to true, mat-expansion-panel is set to open, by default is value is false.

How do I change the toggle icon on my mat-expansion-panel?

The default icon for toggling a mat-expansion-panel is > . Setting hideToggle true just hides the toggle icon.


1 Answers

Demo

This is not something that is supported natively. Though you can quickly build a work-around in a few steps:

First you need to remove the indicator since it is not something the user will be able to interact with:

<mat-expansion-panel #panel1 [hideToggle]="true">

Note that I also gave the component a name in order to get its reference later on.

To prevent user interactions we will add a css class prevent-click:

<mat-accordion class="prevent-click">

.prevent-click{
  pointer-events: none;
}

If you want the user to be able to interact with the content of the panels:

<button class="authorize-click" mat-raised-button>test</button>

.authorize-click{
  pointer-events: auto;
}

NB: You can wrap the content of the expansion panel with this class to allow any interaction within.


To interact with the panels you use the reference of the panels (see above). You can call 3 functions

  • open()
  • close()
  • toggle()

<button (click)="panel1.toggle()">Toggle panel 1</button>


If you want to interact with the panels from your component, you need to get their references using ViewChild().

Example:

@ViewChild('panel1') firstPanel: MatExpansionPanel;

public toggleFirstPanel(){
  this.firstPanel.toggle();
}
like image 116
Ploppy Avatar answered Oct 25 '22 22:10

Ploppy