2

我想以编程方式控制何时显示和隐藏对话框。除非使用“X”关闭对话框并添加 ajax 关闭事件侦听器,否则它是有效的。例如,在下面的代码中,如果我注释掉 ajax 行,则使用“X”关闭对话框并使用按钮多次显示/重新打开。

顺便说一句:我已经看到了使用 oncomplete 方法调用欺骗的 javascript 选项。

<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

4

1 回答 1

6

我认为更新visible属性不是打开/关闭对话框的正确方法。它应该是这样的:

RequestContext context = RequestContext.getCurrentInstance();
context.execute("dlg.show();"); // To open the dialog
context.execute("dlg.hide();"); // To close the dialog
于 2013-06-30T07:59:16.277 回答