Tanooki
Tanooki

Reputation: 41

Keep the parameters from managed Bean to a dialog open in PrimeFaces form using JSF

I have a problem training with forms in PrimeFaces. I will try to explain in the best way:

The functional idea is open a dialog in which the user can see certain inputs introduced previously.

The fact is that I need to use (save) some properties from bean which I'm managing in the view when the dialog is opened.

Example in code:

My xhtml says:

    </h:form>

        <!-- Code Here -->

        <p:commandButton value="Preview" action="#{mockBean.showPreview}"/>

        <!-- Code Here -->

        <p:dialog id="myDialog" 
                  widgetVar="myDialog" 
                  modal="true" 
                  appendToBody="true" 
                  resizable="false">
            <h:form>                        
                <p:panelGrid columns="2">

                    <!-- ...    Code Here -->   

                    <h:outputText value="Name" />
                    <h:outputText value="#{mockBean.name}" 
                                  style="display:block; 
                                  width:225px"/>    

                    <!-- ... Code Here -->

                </p:panelGrid>
            </h:form>
        </p:dialog>
    </h:form>

My Bean says: (MockBean.java)

<!-- ... Code Here -->

public void showPreview(){
    RequestContext context = RequestContext.getCurrentInstance(); 
    context.execute("myDialog.show()");
}

<!-- ... Code Here -->

The mockBean.name property will be empty in dialog when, previously, and later, in view, the bean has a correct value

I suspect that the solution will be easy but i'm newbie in this technologies and just'm introducing to this.

Sorry if the question is absurd in part or something like that.

Greetings.

Upvotes: 0

Views: 3845

Answers (2)

Qadir Hussain
Qadir Hussain

Reputation: 1263

If you just want to display some message from backing bean then there is no need to use another <h:form> tag inside a <h:form> tag. Your code should look something like this.

<h:form>

        <!-- Code Here -->

        <p:commandButton value="Preview" action="#{mockBean.showPreview}" update="myDialog" />

        <!-- Code Here -->
    <p:dialog id="myDialog" 
          widgetVar="myDialog" 
          modal="true" 
          appendToBody="true" 
          resizable="false">

       <p:panelGrid columns="2">

            <!--  Code Here --> 

            <h:outputText value="Name" />
            <h:outputText value="#{mockBean.nombre}"  
                    width:225px"/>    

            <!-- ... Code Here -->

       </p:panelGrid>

    </p:dialog>

</h:form>

And your java code should be as.

public void showPreview(){
    RequestContext context = RequestContext.getCurrentInstance(); 
    context.execute("PF('myDialog').show();");
}

Upvotes: 0

Pellizon
Pellizon

Reputation: 1375

You must update the Dialog, so it can read the saved values in your bean.

The Dialog is static, it is filled when you page is rendered, and it doesn't render again unless you tell him to do so.

You could use the actionListener of your commandButton to save the properties, combined with the onClick="PF('myDialog').show()" and update="myDialog"

Something like this:

<p:commandButton value="Save and Open Dialog" actionListener="#{mockBean.saveProperties()}" update="myDialog" onclick="PF('myDialog').show()"/>

Buuut, Since you are oppening the dialog from your bean, you could just try the code bellow:

 public void showPreview(){
    RequestContext context = RequestContext.getCurrentInstance(); 
    context.execute("myDialog.show()");
    context.update("myDialog");
}

Upvotes: 3

Related Questions