2017-10-09 64 views
0

您好,我正在尝试解决此问题。希望您能够帮助我。如果表格未填满,Primefaces将打开确认对话框

我有一个问题形式与optionals问题(textfields,复选框,单选按钮)。我试图打开一个确认对话框(“有一些问题没有回答,你确定要继续?是/否”),只要有问题没有填写。我可以打开对话框来要求确认。但我不能验证对话框是否必须显示或必须跳过。

  <h:form> 
       <div class="col-xs-12 ctg-home-button center"> 
        <p:commandButton 
         id="cancel-button" 
         actionListener="#{answerSurveyView.restart}" 
         immediate="true" 
         styleClass="btn btn-default" 
         title="#{propertiesBean.getProperty('answer.cancel')}" 
         value="#{propertiesBean.getProperty('answer.cancel')}" 
         /> 
        <h:outputText value="&#160;" /> 
        <p:commandButton 
         id="answer-button" 
         action="#{answerSurveyView.saveAnswers}" 
         styleClass="btn btn-default" 
         title="#{propertiesBean.getProperty('answer.send')}" 
         update="answer-form" 
         value="#{propertiesBean.getProperty('answer.send')}" 
         > 
         <p:confirm header="Confirmation" message="#{propertiesBean.getProperty('answer.survey-confirmation')}" icon="ui-icon-alert" /> 
        </p:commandButton> 
       </div> 
       <p:confirmDialog global="true" showEffect="fade" hideEffect="fade"> 
        <p:commandButton value="#{propertiesBean.getProperty('answer.send')}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" /> 
        <p:commandButton value="#{propertiesBean.getProperty('answer.close')}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" /> 
       </p:confirmDialog> 
      </h:form> 

回答

1

使用您confirmDialogrendered属性来指示是否要渲染与否。设置为rendered属性从视图返回的值,指示是否存在尚未回答的问题。

每次用户在事件中使用ajax来回答问题时,请重新加载您的confirmDialog,调用视图上的方法重新计算unansweredQuestions属性;和update属性与您的confirmDialog id为值,因此您可以将rendered设置为false一旦最后一个问题被回答。 举例来说,如果你使用的是下拉列表给出的答案选项,页面的代码可能是这样的:

<div class="col-xs-12 ctg-home-button center"> 
     <p:commandButton .... 
     ..... 
    </div> 

    <p:selectOneMenu value="#{answerSurveyView.firstQuestionAnswer}"> 
     <p:ajax listener="#{dropdownView.onAnswerSelect}" update="myConfirm" /> 
     <f:selectItem itemLabel="Select option" itemValue="" noSelectionOption="true" /> 
     <f:selectItems value="#{answerSurveyView.firstQuestionAnswerOptions}" /> 
    </p:selectOneMenu> 

    <p:confirmDialog id='myConfirm' rendered="#{answerSurveyView.unansweredQuestions}" global="true" showEffect="fade" hideEffect="fade"> 
     <p:commandButton value="#{propertiesBean.getProperty('answer.send')}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" /> 
     <p:commandButton value="#{propertiesBean.getProperty('answer.close')}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" /> 
    </p:confirmDialog> 

和代码在您的视图:

// declare unansweredQuestions attribute and set it to false 
private boolean unansweredQuestions = false; 

// unansweredQuestions getter 
public boolean getUnansweredQuestions() { 
    return unansweredQuestions; 
} 

// call this method every time a question is answered 
// to update the unansweredQuestions value 
public void onAnswerSelect() { 
    unansweredQuestions = foo(); //method returns true if there are questions left and false if not. 
}