【问题标题】:JSF Conditionally render errors in a rowJSF 有条件地连续呈现错误
【发布时间】:2015-09-20 01:45:48
【问题描述】:

我想在此表的单独一行中呈现错误。但是,当我使用 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>

【问题讨论】:

  • 您必须指定具体的id,这将根据迭代索引是唯一的。您可以使用像 &lt;p:dataGrid&gt; 这样的迭代组件来实现相同的目的,而不是使用杂散的 &lt;ui:repeat&gt; 手动生成 &lt;tr&gt;
  • 或者在这种情况下使用c:foreach(但看看它是否没有副作用)

标签: jsf jsf-2 primefaces jsf-2.2


【解决方案1】:

在 ajax 更新时调用 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>

【讨论】:

    猜你喜欢
    • 2011-11-19
    • 2023-02-21
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2023-04-07
    • 2018-03-11
    相关资源
    最近更新 更多