Search code examples
jsfprimefacesfacelets

PrimeFaces dialog refer to parent


I have an xhtml page that display a Datatable with entries. I also have a button to insert a new entry, that displays a dialog that has a form. The insertion form is used as <ui:include> in the parent .xhtml as follows:

Parent file

    <h:form id="mainForm">
        <p:commandButton process="@form" update=":dlgGrp" oncomplete="dlg.show()"/>

        <p:datatable id = "datatable"> 

            various columns
            .....
            .....
        </p:datatable>

    </h:form>

    <p:dialog widgetVar="dlg" >

        <h:panelGroup id="dlgGrp">
            <ui:include src="include.xhtml" />
        </h:panelGroup>
    </p:dialog>

</ui:composition>

Dialog file

<ui:composition xmlns . . .>

    <h:form id="subForm">

        various input fields
        ......  
        ......
        <p:commandButton process="@form" update=":mainForm" oncomplete="dlg.hide()"/>

    </h:form>

</ui:composition>

How can I refer generically to a parent component as show in the file. In a few words as soon as I hit the submit button in the dialog, I want to update the main form and hide the dialog. These components however are in the "parent field".

This should probably be done programatically through the backing bean, since I do not want to include parent-specific actions in the child .xhtml, since I may aslo want to use it as a standalone .xhtml.


Solution

  • If you want to update the mainForm through a method in the backingBean. Just modify

    <p:commandButton process="@form" action="#{backingBean.method}" oncomplete="dlg.hide()"/>
    

    and in your backingBean do the following (see Primefaces Showcase):

    public void method() {
      // do something
      RequestContext context = RequestContext.getCurrentInstance();
      context.update(":mainForm");
    }
    

    You just need to check, if your mainForm is already in another NamingContainer. If so, you need to change the context.update(...) accordingly.

    Furthermore, if you want to update the mainForm from your backing bean, I would also recomment hiding the dialog in the backingBean, depending on the input processed. If e.g. some data is invalid, you don't want to hide the dialog. That cannot be done currently with your oncomplete action which is executed automatically after receiving the response from the server. The dialog would get close whether the input was correct or not.

    So add to the method():

    if (everythingWentFine) {
      context.execute("dlg.hide();");
    }
    

    and remove the oncomplete from your p:commandButton.