Close Dialog On Success of Action

UI Components for JSF
Post Reply
wheeliea
Posts: 14
Joined: 23 Jun 2014, 15:17

14 Apr 2015, 22:01

I have a Primefaces 5.1 application. I have a form with several panels, I also have a dialog with it's own form. The trouble I am having is that the dialog remains open after I have forced an error and displayed it using the p:messages. The dialog closes properly if I have no errors. The purpose of the dialog is to allow a user to choose a 'template'. If none is selected and user clicks Apply Template the error message displays accordingly. After that if the user chooses a template the dialog does not close. How can I get the dialog to close after an error?

xhtml:

Code: Select all

         <p:dialog
            width="800"
            id="templateDialog"
            widgetVar="templateBox"
            header="Choose Template"
            closable="false"
            modal="true"
            draggable="true"
            showEffect="clip"
            hideEffect="fade"
            resizable="false"
            dynamic="true"
            visible="#{manageWorkflowBean.templateDialogVisible}">
            
            <h:form id="templateForm">

               <p:messages
                  autoUpdate="false"
                  globalOnly="true"
                  closable="true"
                  showDetail="false" />


               <p:dataTable
                  id="templateTable"
                  var="templateItem"
                  lazy="true"
                  rowKey="#{templateItem.templateId}"
                  value="#{manageWorkflowBean.templates}"
                  widgetVar="templateTable"
                  emptyMessage="No Rows Found"
                  rows="10"
                  paginator="true"
                  paginatorAlwaysVisible="true"
                  paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                  rowsPerPageTemplate="5,10,15,25"
                  tableStyleClass="usertable"
                  selection="#{manageWorkflowBean.selectedTemplate}">

                  <p:column
                     headerText="Select"
                     selectionMode="single">
                  </p:column>
                  
                  <p:column
                     headerText="Template Name"
                     style="text-align:left;">
                     <h:outputText value="#{templateItem.templateName}" />
                  </p:column>

                  <p:column
                     headerText="Agency"
                     style="text-align:left;">
                     <h:outputText value="#{templateItem.agency}" />
                  </p:column>

               </p:dataTable>
               <br/>

               <div
                  id="templateButtons"
                  style="margin: auto; text-align: center">

                  <p:commandButton
                     id="selectButton"
                     value="Apply Template"
                     actionListener="#{manageWorkflowBean.selectTemplate}"
                     process="@form"
                     update=":wfForm:selectedTemplateLabel templateForm" />
                  <p:commandButton
                     id="cancelButton"
                     value="#{labels['cancel']}"
                     process="@this"
                     update="@form"
                     immediate="true"
                     actionListener="#{manageWorkflowBean.cancelSelectTemplate}"
                     oncomplete="PF('templateBox').hide()">
                     <p:resetInput target=":wfForm:selectedTemplateLabel"></p:resetInput>
                  </p:commandButton>
               </div>
            </h:form>
         </p:dialog>
managed bean method:

Code: Select all

   public void selectTemplate(ActionEvent event) {
      if (this.selectedTemplate == null) {
         FacesContext.getCurrentInstance().addMessage(
                  null,
                  new FacesMessage(
                        FacesMessage.SEVERITY_ERROR, "A template must be selected", ""));
         FacesContext.getCurrentInstance().renderResponse();
         return;
      } else {
         this.selectedTemplateName = this.selectedTemplate.getTemplateName();
         this.templateDialogVisible = false;
      }
   }

Jan Eckert
Posts: 84
Joined: 11 Sep 2014, 10:13
Location: Brussels, Belgium

16 Apr 2015, 10:26

Good effort, but please rework your problem case in a working example so that we can try to re-create your problem.

http://stackoverflow.com/help/mcve
Primefaces 6.1+
Wildfly 11

nilsonuehara
Posts: 16
Joined: 30 Aug 2011, 19:17

16 Apr 2015, 14:59

You can close your dialog in the ManagedBean with this code:

Code: Select all

RequestContext.getCurrentInstance().execute("PF('templateBox').hide()");

User avatar
sudheer
PrimeFaces Core Developer
Posts: 4345
Joined: 16 Oct 2011, 19:19
Location: Singapore

17 Apr 2015, 11:17

If you don't want to invoke clientside API from RequestContext then you can use RequestContext callbackparameters and get the clientside API calls in JS itself.PageNo 579
Author,Speaker
https://twitter.com/sudheerjonna
Github: https://github.com/sudheerj
Website http://sudheerjonna.com/

___________________
Sudheer Jonna

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 74 guests