从 Managed Bean 函数调用 Primefaces 对话框 [英] Calling Primefaces dialog box from Managed Bean function

查看:22
本文介绍了从 Managed Bean 函数调用 Primefaces 对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个带有一些功能的托管 bean,根据该功能中的某些条件,我想调用一个对话框

Hi I have a managed bean with some functions , based on some condition in that function I will like to call a dialog box

托管 bean 函数如下

Managed bean function goes as

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");
    }
}

有什么方法可以让我们从这样的托管函数中调用对话框

Is there any method using which we can call the dialog box from such managed function

推荐答案

您可以通过使用 RequestContext(或 PrimeFaces 如果您使用的是 6.2 或更高版本)类.

You can, by using the RequestContext (or PrimeFaces if you are using the version 6.2 or higher) class.

假设你有以下:

<p:dialog id="myDialogID" widgetVar="myDialogVar">
....
</p:dialog>

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

So the way you do in the facelet itself, i.e. onclick=myDialogVar.show();, the same can be done in your managed bean like so:

对于 PrimeFaces <= 3.x

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

对于 PrimeFaces >= 4.x 到 PrimeFaces <6.2(根据@dognose 和@Sujan)

For PrimeFaces >= 4.x to PrimeFaces < 6.2 (as per @dognose and @Sujan)

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

对于 PrimeFaces >= 6.2

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

这是为了使用目标对话框.如果您只需要显示一条消息而不优先考虑任何自定义对话框,那么您可以这样做:

This is for using targeted dialogs. If you just need to show a message without giving preference to any custom dialog, then you can do it this way:

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);

您也可以传入参数并设置回调.请参阅链接中的展示示例.

You can pass in arguments and set callbacks as well. Refer to the showcase examples in the link.

另请参阅:

这篇关于从 Managed Bean 函数调用 Primefaces 对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆