Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close all Dialogboxes when click ESCAPE(ESC) in primefaces

I am working with 4-5 primefaces dialogboxes. When click ESC i want close all dialogboxes which are opened.

like image 820
user1776313 Avatar asked Oct 26 '12 06:10

user1776313


2 Answers

Call the dialog's hide() function through the client side object specified with widgetVar. So if you defined your p:dialog like:

<p:dialog widgetVar="dialog1" header="Dialog 1"/>
<p:dialog widgetVar="dialog2" header="Dialog 2"/>

Your ESC button should look like:

<p:commandButton value="ESC" onclick="dialog1.hide();dialog2.hide()"/>

You can also create a reusable p:remoteCommand to close all your dialog and use that in your p:commandButton or in p:hotkey - if by "click ESCAPE" you mean hittig the Escape button:

<p:remoteCommand name="closeAll" onsuccess="dialog1.hide();dialog2.hide()"/>

then in your components refer to the closeAll() command:

<p:hotkey bind="esc" handler="closeAll()"/>
<p:commandButton value="ESC" onclick="closeAll()"/>
like image 164
Akos K Avatar answered Oct 31 '22 19:10

Akos K


Although the post is old, but the answer is rather a static solution, here's a dynamic one using jQuery.

function escDialog() {
   $(document).keyup(function(e) {
       if (e.keyCode == 27) { // esc code is 27 
           closeAllDialog() ;
       }   
   });
}

function closeAllDialog() {
   for (var propertyName in PrimeFaces.widgets) {
     if (PrimeFaces.widgets[propertyName] instanceof PrimeFaces.widget.Dialog ||
         PrimeFaces.widgets[propertyName] instanceof PrimeFaces.widget.LightBox) {
         PrimeFaces.widgets[propertyName].hide();
     }
   }
}

Then in your document.ready you would call escDialog()

Hope this helps.

like image 44
Hatem Alimam Avatar answered Oct 31 '22 19:10

Hatem Alimam