jsf - 如何在 panelGrid 中插入条件行,同时保持行简洁?

标签 jsf

我有一个h:panelGrid有两列和不同的行:

<h:panelGrid columns="2">
    <p:outputLabel for="attr1" value="#{messages.attr1}" />
    <p:inputText id="attr1" value="#{viewBean.attr1}" />
    <p:outputLabel for="attr2" value="#{messages.attr2}" />
    <p:inputText id="attr2" value="#{viewBean.attr2}">
    <p:outputLabel for="attr3" value="#{messages.attr3}" />
    <p:inputText id="attr3" value="#{viewBean.attr3}" />
</h:panelGrid>

现在,某些行只能有条件地出现。在示例中,这可能是 attr2 和 attr3。当我构建 <ui:fragment />在它们周围,条件渲染有效(见下文),但是 jsf 尝试将整个 block 放入一个 <td /> 中。表格单元格元素。

<h:panelGrid columns="2">
    <p:outputLabel for="attr1" value="#{messages.attr1}" />
    <p:inputText id="attr1" value="#{viewBean.attr1}" />
    <ui:fragment rendered="#{viewBean.myCondition}">
        <p:outputLabel for="attr2" value="#{messages.attr2}" />
        <p:inputText id="attr2" value="#{viewBean.attr2}">
        <p:outputLabel for="attr3" value="#{messages.attr3}" />
        <p:inputText id="attr3" value="#{viewBean.attr3}" />
    </ui:fragment>
</h:panelGrid>

另一个选择是创建两个 panelGrid。一个用于第一个 block ,一个用于第二个 block :

<h:panelGrid columns="2">
    <p:outputLabel for="attr1" value="#{messages.attr1}" />
    <p:inputText id="attr1" value="#{viewBean.attr1}" />
</h:panelGrid>
<h:panelGrid columns="2">
    <ui:fragment rendered="#{viewBean.myCondition}">
        <p:outputLabel for="attr2" value="#{messages.attr2}" />
        <p:inputText id="attr2" value="#{viewBean.attr2}">
        <p:outputLabel for="attr3" value="#{messages.attr3}" />
        <p:inputText id="attr3" value="#{viewBean.attr3}" />
    </ui:fragment>
</h:panelGrid>

这里的问题是,两个网格中的列现在不再简洁。有什么办法可以避免这个问题?

请注意,我不止三行,因此输入 rendered每个 outputLabel 的属性和inputText元素会非常乏味。

最佳答案

<h:panelGrid> 渲染器仅考虑直接 UIComponent子项作为表格单元格,而不是更深层次的嵌套单元格。 <ui:fragment>实际上也是一个UIComponent 。因此,它成为一个包含所有嵌套子项的单个表格单元格。

您有 2 个选项,具体取决于 rendered 的来源条件:

  1. 如果它在 View 构建期间可用,请使用 <c:if>相反。

    <h:panelGrid columns="2">
        <p:outputLabel ... />
        <p:inputText ... />
        <c:if test="#{viewBean.myCondition}">
            <p:outputLabel ... />
            <p:inputText ... />
            <p:outputLabel ... />
            <p:inputText ... />
        </c:if>
    </h:panelGrid>
    
  2. 否则,如果不能保证在 View 构建期间可用,请删除 <ui:fragment>并重复rendered children 的状况。

    <h:panelGrid columns="2">
        <p:outputLabel ... />
        <p:inputText ... />
        <p:outputLabel ... rendered="#{viewBean.myCondition}" />
        <p:inputText ... rendered="#{viewBean.myCondition}" />
        <p:outputLabel ... rendered="#{viewBean.myCondition}" />
        <p:inputText ... rendered="#{viewBean.myCondition}" />
    </h:panelGrid>
    

另请参阅:

关于jsf - 如何在 panelGrid 中插入条件行,同时保持行简洁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765538/

相关文章:

java - 密码错误时登录表单不显示验证消息

spring - 将 Spring bean 注入(inject) JSF ManagedBean 时出错

jsf - Primefaces 数据表的列标题中的工具提示

jsf - 如何在 JSF 中跨回发保持滚动位置?

java - MVC 和 JavaServer Faces

java - 在 JSF-2.0 中使用 JSP 技术(而不是 XHTML)有什么缺点吗?

java - a4j 如何轮询不刷新 Chrome 中的图像

jsf - 使用 "Please select"f :selectItem with null/empty value inside a p:selectOneMenu

java - <f :loadBundle basename Non-serializable attribute

java - Primefaces 仅阻止组件一次