2

I would like to programmatically control when a dialog is showed and hidden. It's works except when the dialog is closed using 'X' and and ajax close event listener is added. For example in the code below close dialog using 'X' and show/reopen many times using the button if I comment out the ajax line.

BTW: I have seen the javascript option which is tricked using oncomplete method call.

<h:form>
Status: <h:outputText id="status" value="#{helloBean.visible}" />
<p />

<p:dialog id="myDialog" header="Header Text" widgetVar="dlg" visible="#{helloBean.visible}">
<p:ajax event="close" listener="#{helloBean.hide}" update="myDialog" />

<h1>Dialog content ....</h1>

  <p:commandButton value="Hide" actionListener="#{helloBean.hide}" update="myDialog status" />
</p:dialog>

<p:commandButton value="Show" actionListener="#{helloBean.show}" update="myDialog status" />            
</h:form>


@ManagedBean
@ViewScoped
public class HelloBean implements Serializable {

private static final long serialVersionUID = 1L;
private boolean visible;

public boolean isVisible() {
  return visible;
}

public void setVisible(boolean visible) {
  this.visible = visible;
}

public void show() {
  setVisible(true);
  System.err.println("show(): " + visible);
}
public void hide() {
  setVisible(false);
  System.err.println("hide(): " + visible);
}
}

Primefaces 3.5, JSF 2.0.7, Tomcat 7

billyg
  • 23
  • 1
  • 4

1 Answers1

6

I think updating the visible attribute is not a correct way to open/close a dialog. It should be something like this:

RequestContext context = RequestContext.getCurrentInstance();
context.execute("dlg.show();"); // To open the dialog
context.execute("dlg.hide();"); // To close the dialog
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • Thanks for posting this answer. Looks like the visible attribute is used to decide if the dialog should be displayed but is not used to close it if it is already open. – 8bitjunkie Jul 16 '13 at 11:52
  • "I think updating the visible attribute is not a correct way to open/close a dialog. " Well, @BalusC reported that he had it working in the PF forums: http://forum.primefaces.org/viewtopic.php?f=3&t=16769 – mattalxndr Nov 19 '15 at 17:42
  • As of Primefaces V7, the execute(String) from the type RequestContext is deprecated. – EnigmaTech Jun 10 '21 at 10:24