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.
Prevent Clicking the toggle in perticular button in mat-expansion-panel-header. using this you can prevent clicking on perticular element (click)="! exppanel.
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.
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.
The default icon for toggling a mat-expansion-panel is > . Setting hideToggle true just hides the toggle icon.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With