【问题标题】:Putting <span> inside a <p:panelGrid> causes "weird" display将 <span> 放入 <p:panelGrid> 会导致“奇怪”显示
【发布时间】:2015-06-22 09:29:10
【问题描述】:

我想创建带有 ajax 更新事件的动态表单。因此动态ID很重要。但是,将 id 属性绑定到 bean 值会导致 Empty Id 异常。我看到了建议使用 html 的 stackoverflow 问题之一。所以我使用了跨度。

xhtml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui" template="../../templates/ui.xhtml">

    <ui:define name="content">
        <h:form id="testform">
            <ui:repeat value="#{repeat.questions}" var="question">
                <p:panelGrid columns="2">
                    <p:outputLabel value="#{question.label}"/>

                    <span id="#{question.questionCode}">
                        <p:inputText value="#{repeat.values[question.questionCode]}"
                                     rendered="#{question.type == 'TEXT'}">
                            <p:ajax event="blur" update="#{question.dependentQuestionCode}"
                                    disabled="#{empty question.dependentQuestionCode}"/>
                        </p:inputText>

                        <p:password value="#{repeat.values[question.questionCode]}"
                                    rendered="#{question.type == 'SECRET'}">
                        </p:password>

                        <p:inputTextarea value="#{repeat.values[question.questionCode]}"
                                         rendered="#{question.type == 'TEXTAREA'}">
                        </p:inputTextarea>

                        <p:selectOneRadio value="#{repeat.values[question.questionCode]}"
                                          rendered="#{question.type == 'RADIO'}" layout="grid" columns="1">
                            <f:selectItems value="#{question.options}"/>
                        </p:selectOneRadio>

                        <p:selectOneMenu value="#{repeat.values[question.questionCode]}"
                                         rendered="#{question.type == 'SELECTONE'}">
                            <f:selectItems value="#{question.options}"/>
                        </p:selectOneMenu>

                        <p:selectManyMenu value="#{repeat.values[question.questionCode]}"
                                          rendered="#{question.type == 'SELECTMANY'}">
                            <f:selectItems value="#{question.options}"/>
                        </p:selectManyMenu>

                        <p:selectBooleanCheckbox
                                value="#{repeat.values[question.questionCode]}"
                                rendered="#{question.type == 'CHECKONE'}"/>

                        <p:selectManyCheckbox value="#{repeat.values[question.questionCode]}"
                                              rendered="#{question.type == 'CHECKMANY'}">
                            <f:selectItems value="#{question.options}"/>
                        </p:selectManyCheckbox>
                     </span>
                </p:panelGrid>
            </ui:repeat>

            <p:commandButton value="Submit" actionListener="#{repeat.submit}"/>
        </h:form>
    </ui:define>
</ui:composition>

但是,显示很奇怪。它在每次迭代中关闭了跨度。为什么跨度关闭标签之前跨度关闭不合理?

【问题讨论】:

    标签: jsf-2 primefaces panelgrid


    【解决方案1】:

    &lt;h|p:panelGrid&gt; 仅将真正的 JSF 子组件划分为列,而不是普通的 HTML 元素。

    &lt;span&gt;(或更好的&lt;div&gt;)作为&lt;ui:repeat&gt; 的直接子级。

    <ui:repeat value="#{bean.questions}" var="question">
        <div id="#{question.code}">
            <p:panelGrid>
                ...
            </p:panelGrid>
        </div>
    </ui:repeat>
    

    【讨论】:

    • 我尝试用update="@(.#{question.dependentQuestionCode})" 更新&lt;div id="#{question.questionCode}" class="#{question.questionCode}"&gt;,但它无法更新依赖的div。但是,当我尝试其中一个 update=":testform:#{question.dependentQuestionCode}"update="@(form)" 时,它可能会更新。但是,通过父表单中的确切 id 更新依赖 div 不是一个好方法,因为我不知道它将使用的表单 id。更新整个表单又是一个非常糟糕的选择。有没有办法相对更新组件?
    • &lt;div&gt; 不是 JSF 组件。而是参考一个真正的 JSF 组件。例如。 &lt;p:panelGrid&gt;.
    • 嗯,其实我来这里是因为需要动态id来更新特定的组件。选择类(primefaces 中的 styleClass)也解决了我之前的动态 id 问题。另一方面,它让我遇到了另一个问题。如果我静态输入update="@(.q5)" 它会更新,但是如果我把update="@(.#{question.dependentQuestionCode})" ajax 挂起几秒钟并抛出异常Error: Syntax error, unrecognized expression: . 抱歉有很多问题,但我找不到调试ajax 更新js 代码的方法场景。
    • 顺便说一句,正如您所建议的,我更改了&lt;div&gt; 部分(实际上已删除并将 styleClass 添加到 panelGrid):&lt;ui:repeat value="#{repeat.questions}" var="question"&gt; &lt;p:panelGrid columns="2" styleClass="#{question.questionCode}"&gt; &lt;p:outputLabel value="#{question.label}"/&gt;
    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多