Calling Primefaces dialog box from Managed Bean function

前端 未结 3 1405
无人共我
无人共我 2020-12-05 02:41

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

Managed bean function goes as



        
3条回答
  •  我在风中等你
    2020-12-05 03:13

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

    Suppose you have the following:

    
    ....
    
    

    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:

    For PrimeFaces <= 3.x

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

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

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

    For 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.

    See also:

    • Primefaces Dialog framework

提交回复
热议问题