Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent angular material mat-menu from closing?

I'm creating a date time picker control in the angular material and having the below code to do that

<button mat-button [matMenuTriggerFor]="menu">     <mat-icon>date_range</mat-icon>     <span>Date Range</span> </button> <mat-menu #menu="matMenu">     <div fxLayout="row">         <div fxLayout="column">             <button (click)="setInterval(15)" mat-menu-item>Last 15 minutes</button>             <button (click)="setInterval(360)" mat-menu-item>Last 6 hours</button>             <button (click)="setInterval(1440)" mat-menu-item>Last 24 hours</button>             <button (click)="setInterval(2880)" mat-menu-item>Last 2 days</button>             <button (click)="setInterval(10080)" mat-menu-item>Last 7 days</button>             <button (click)="setInterval(-1)" [matMenuTriggerFor]="dateTimeMenu" mat-menu-item>Custom</button>         </div>         <mat-menu class="date-range-menu" #dateTimeMenu="matMenu">             <div fxLayout="row">                 <div fxLayout="column">                     <b>From</b>                     <mat-calendar></mat-calendar>                 </div>                 <div fxLayout="column">                     <b>To</b>                     <mat-calendar></mat-calendar>                 </div>             </div>         </mat-menu>     </div> </mat-menu> 

Currently when ever I click a button it is closing the menu. I know we can do $event.stoppropagation() on each mat-menu-item to prevent it from closing.

But I want to know is it possible to do that for mat-calendar

enter image description here

As you can see in the above image currently when i select a date it is closing the menu. Is it possible to prevent that?

like image 882
Umamaheswaran Avatar asked Oct 27 '17 05:10

Umamaheswaran


People also ask

How do I stop mat menu closing?

stoppropagation() on each mat-menu-item to prevent it from closing. As you can see in the above image currently when i select a date it is closing the menu.

How do you stop angular mat menu from closing when you click outside of it?

Show activity on this post. this should be the real answer, but just to add one extra note you need to set the [hasBackdrop]="false", in order to prevent the menu from closing when click outside of it.

What is mat menu angular?

To implement menu items in Angular we can use angular material menu module called MatMenuModule . mat-menu selector is used to display floating menu panel containing list of menu items.

How to close a mat dialog programmatically in angular?

There are two ways to close a mat dialog programmatically in Angular. in your dialog’s constructor. which closes the mat dialog when it gets called. If you want to close the mat dialog from its parent component i.e. the component which has called the mat dialog, you can make use of the dialogRef to close it programmatically.

How to implement menu items in angular?

To implement menu items in Angular we can use angular material menu module called MatMenuModule. mat-menu selector is used to display floating menu panel containing list of menu items.

How to stop mat-menu from closing when click outside of it?

My question is how to stop mat-menu from closing when you click OUTSIDE of it (menu). The click is handled by the overlay backdrop. You can apply/remove classes to the backdrop dynamically based on your menu open and close, and defeat the backdrop click using CSS with pointer-events.

How to navigate to a particular route using matmenulistitem in angular?

If you are using angular routes in your application we can navigate to that particular route on mat menu click event. Or we can add another property to the MatMenuListItem which represents angular route to navigate so that we can avoid the above if loop check.


1 Answers

You just add (click) = "$event.stopPropagation()" to the parent element of these calendars. Like below,

<mat-menu class="date-range-menu" #dateTimeMenu="matMenu">     <div fxLayout="row">         <div fxLayout="column" (click)="$event.stopPropagation();">             <b>From</b>             <mat-calendar></mat-calendar>         </div>         <div fxLayout="column" (click)="$event.stopPropagation();">             <b>To</b>             <mat-calendar></mat-calendar>         </div>     </div> </mat-menu> 

Stackblitz demo.

like image 91
Anik Avatar answered Sep 23 '22 01:09

Anik