2017-10-09 43 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. 
}