JSF有条件地连续渲染错误

时间:2015-07-02 13:34:45

标签: jsf jsf-2 primefaces jsf-2.2

我想在此表的单独行中呈现错误。但是,当我使用ui:repeat时,绑定不起作用,因为它将所有行设置为相同的值。

         <ui:repeat id="quest" value="#{questions}" var="question">
            <tr>

               <td class="col-md-2">
                     <p:selectOneRadio id="questionId" value="#{question.response}" binding="#{questionBinding}" validator="#{question.validate}" required="true" requiredMessage="You must answer the question to continue">
                        <f:selectItem itemLabel="Yes" itemValue="Yes" />
                        <f:selectItem itemLabel="No" itemValue="No" />
                        <p:ajax update="error-row" />
                     </p:selectOneRadio>
              </td>


            </tr>
            <h:panelGroup id="error-row">
              <ui:fragment rendered="#{not empty facesContext.getMessageList(questionBinding.clientId)}">
                <tr>
                    <td colspan="2"><p:message for="questionId" id="msgQuestion" /></td>
                </tr>
              </ui:fragment>
            </h:panelGroup>
        </ui:repeat>

1 个答案:

答案 0 :(得分:0)

在ajax update上调用javascript:

....
        <p:ajax update="msgQuestion" oncomplete="clearEmptyRows()" />
....

使用javascript函数:

 <script>
            function clearEmptyRows() {
                $(".error-row").each(function(index) {
                    if ($(this).text().trim() == '') {
                        $(this).css("display", "none");
                    } else {
                        $(this).css("display", "table-row");
                    }
                });
            }
            $(document).ready(function() {
                clearEmptyRows();
            });
    </script>