从托管Bean函数调用Primefaces对话框框。

30

你好,我有一个包含一些函数的托管 Bean,在这些函数中根据某些条件,我想调用一个对话框。

托管 Bean 函数如下:

public String editStudent(){    
    setReadOnly(false);     
    setButton(true, true, true, false, true, true,true);
    LockItem lItem;
    if(selectStudent !=null){
        lItem = (LockItem) services.getbyId("LockItem", condition);
        if (lItem == null){
            System.out.println("Student Avalibale for process :::"); 
            studentRedirect();
            return "studentEdit.jsf?faces-redirect=true";
        } else {
             //To show dialog from here
             System.out.println("Student Not Avalibale : Locked By " + lItem.getLockedBy());
        }
    } else {
        FacesMessage msg;
        msg = new FacesMessage("Please select the record.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return show("menu");
    }
}

有没有一种方法可以在托管函数中调用对话框框?

3个回答

99
你可以使用 RequestContext 类(如果你使用的是版本6.2或更高,则可以使用 PrimeFaces 类)。
假设你有以下内容:
<p:dialog id="myDialogID" widgetVar="myDialogVar">
....
</p:dialog>

因此,在面板本身中进行的方式,即onclick=myDialogVar.show();,同样可以在您的托管bean中完成,如下所示:

对于PrimeFaces <= 3.x

RequestContext context = RequestContext.getCurrentInstance();
context.execute("myDialogVar.show();");

对于 PrimeFaces 版本大于等于 4.x 且小于 6.2(根据 @dognose 和 @Sujan)

RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').show();");

对于PrimeFaces版本 >= 6.2

PrimeFaces current = PrimeFaces.current();
current.executeScript("PF('myDialogVar').show();");

这是用于使用定向对话框的。如果您只需要显示消息而不偏好任何自定义对话框,那么可以按照以下方式操作:

FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message Title", "Message body");

// For PrimeFaces < 6.2
RequestContext.getCurrentInstance().showMessageInDialog(message);

// For PrimeFaces >= 6.2
PrimeFaces.dialog().showMessageDynamic(message);

你可以传入参数并设置回调函数。请参考链接中的展示示例。

另请参阅:


8
如果您正在使用 PrimeFaces 4.0 或更高版本:
RequestContext.getCurrentInstance().execute("PF('yourdialogid').show()");

6

Vrushank的解决方案是正确的。

还有另一种方法:在您的对话框中,将渲染属性绑定到您的bean的布尔值,并将可见属性设置为true,像这样:

<p:confirmDialog
        widgetVar="myDialog"
        visible="true"
        rendered="#{myBean.showMyDialog}">

在你的bean中的action listener中,只需调用setShowMyDialog(true),一个对话框就会显示出来(假设此操作正在更新您的对话框)。如果由于某些原因您不想在用户看不到时呈现对话框的HTML,则此方法非常有用。这样,在bean中,您可以访问信息,以确定您的对话框是否可见。
另一个好处是,当对话框或其父级进行ajax更新时,您的对话框不会被隐藏。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接