多个字段的条件显示 - JSF 2.0 / Primefaces

发布于 2024-10-30 20:26:08 字数 2149 浏览 1 评论 0原文

有条件地显示多个元素(例如依赖于 bean 值的字段列表)的最佳策略是什么?

我想到的可能的解决方案:

  1. JSTL 子句。从我 了解将 JSTL 与 JSF 结合使用是 不鼓励
  2. 使用“渲染” 大多数组件的属性。 不幸的是当我必须处理时 有很多字段就变成了 笨拙地将它们设置在每个元素上......
  3. 将元素放在容器上 并设置渲染属性 在容器上

选项 3 似乎是最明智的,但我不知道使用什么组件来包装这些字段。它需要是一个不影响布局的组件...

我可以使用 span 作为包装器并设置 CSS 可见属性,但字段仍然会被渲染,只是不可见。

有什么想法吗?

更新:

这是一些实际的布局代码。我已经尝试过 & 。使用任何这些标签都会将我的所有字段放入一个 中,我承认,这是有道理的,因为我将一个顶级元素放入我的 panelGrid.

唯一能按照我想要的方式工作的是上面列表中的#2。

    <h:panelGrid columns="2">
        <!-- fields if person -->
        <ui:fragment rendered="#{createEntity.entityType eq 'fizica'}">
            <h:outputLabel value="Prenume: " />
                <h:inputText value="#{createEntity.person.firstName}" />
            <h:outputLabel value="Nume familie: " />
                <h:inputText value="#{createEntity.person.familyName}"  />
            <h:outputLabel value="CNP: " />
                <h:inputText value="#{createEntity.person.cnp}" />
            <h:outputLabel value="Date carte identitate: " />
                <h:inputText value="#{createEntity.person.idCardInfo}" />
            <h:outputLabel value="Cetatenie: " />
                <h:inputText value="#{createEntity.person.country}" />
        </ui:fragment>

        <!--  fields for company  -->
        <ui:fragment rendered="#{createEntity.entityType eq 'juridica'}">           
            <h:outputLabel value="Denumire firma"  />
                <h:inputText value="#{createEntity.company.name}" />
            <h:outputLabel value="CUI"  />
                <h:inputText value="#{createEntity.company.cui}" />
            <h:outputLabel value="Registrul Comertului"  />
                <h:inputText value="#{createEntity.company.regCommerceNo}" />
        </ui:fragment>
    </h:panelGrid>  

What is the best strategy for conditionally displaying multiple elements (for example a list of fields that depends on a bean value)?

Possible solutions I thought of:

  1. JSTL <c:if ... > clause. From what I
    understand using JSTL with JSF is
    discouraged
  2. Using the "rendered"
    attribute of most components.
    Unfortunately when I have to deal
    with a lot of fields it becomes
    clumsy to set them on each one...
  3. Putting the elements on a container
    and setting the rendered attribute
    on the container

Option 3 seems the most sensible but I don't know what component to use to wrap those fields. It needs to be a component that doesn't affect the layout...

I could use a span as a wrapper and set a CSS visible property, but the fields will still be rendered, just invisible.

Any thoughts?

Update:

Here's some actual layout code. I've tried both <h:panelGroup> & <ui:fragment>. Using any of those tags will put all my fields in a single <td>, which I admit, it makes sense because I'm putting a single top level element in my panelGrid.

The only thing that works the way I want is #2 from the list above.

    <h:panelGrid columns="2">
        <!-- fields if person -->
        <ui:fragment rendered="#{createEntity.entityType eq 'fizica'}">
            <h:outputLabel value="Prenume: " />
                <h:inputText value="#{createEntity.person.firstName}" />
            <h:outputLabel value="Nume familie: " />
                <h:inputText value="#{createEntity.person.familyName}"  />
            <h:outputLabel value="CNP: " />
                <h:inputText value="#{createEntity.person.cnp}" />
            <h:outputLabel value="Date carte identitate: " />
                <h:inputText value="#{createEntity.person.idCardInfo}" />
            <h:outputLabel value="Cetatenie: " />
                <h:inputText value="#{createEntity.person.country}" />
        </ui:fragment>

        <!--  fields for company  -->
        <ui:fragment rendered="#{createEntity.entityType eq 'juridica'}">           
            <h:outputLabel value="Denumire firma"  />
                <h:inputText value="#{createEntity.company.name}" />
            <h:outputLabel value="CUI"  />
                <h:inputText value="#{createEntity.company.cui}" />
            <h:outputLabel value="Registrul Comertului"  />
                <h:inputText value="#{createEntity.company.regCommerceNo}" />
        </ui:fragment>
    </h:panelGrid>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月棠 2024-11-06 20:26:08

渲染是显示(或不显示)组件的推荐方式。

如果您可以将它们分组在一起,那么设置容器的渲染属性是完全有效的。我会优先这样做,而不是在每个单独的组件上设置渲染属性。

唯一的小缺点是,如果显示是动态的(您更改支持 bean 值),并且需要重新显示容器,则它不存在。您需要将容器包装在另一个容器中,然后重新渲染。

Rendered is the recommended way to display (or not) a component.

If you can group them together, then setting the rendered attribute of the container is perfectly valid. I'd do that in preference to setting the rendered attribute on each individual component.

The only minor disadvantage is if the display is dynamic (you change the backing bean value), and you need to redisplay the container, it doesn't exist. You need to wrap the container in another container which you then reRender.

剧终人散尽 2024-11-06 20:26:08

您可以使用 h:panelGroup 作为容器。它具有渲染属性并将产生跨度(默认情况下)。

You can use h:panelGroup as a container for this. It has a rendered property and will result in a span (by default).

童话 2024-11-06 20:26:08

我不得不求助于 using ,因为当我将列值包装在另一个组件(如 ui:fragment 或 h:panelGroup)中以使用该组件的渲染时,panelGrid 将整个 ui 片段视为一列,这确实弄乱了我的表格。

<c:if test="#{myBean.displayThese}">
   <!-- columns to display for this condition -->
</c:if>
<c:if test="#{!myBean.displayThese}">
   <!-- other stuff to display -->
</c:if>

令人惊讶的是, 似乎对我不起作用。

I had to resort to using because when I wrapped my column values in another component (like ui:fragment, or h:panelGroup) to use the rendered for that component the panelGrid treated the entire ui fragment as one column which really messed up my table.

<c:if test="#{myBean.displayThese}">
   <!-- columns to display for this condition -->
</c:if>
<c:if test="#{!myBean.displayThese}">
   <!-- other stuff to display -->
</c:if>

surprisingly, <c:otherwise> didn't seem to work for me though.

奢望 2024-11-06 20:26:08

丑陋,但也许对你有用,通过条件风格:

<h:component value="foo" style="#{!myBean.displayFoo ? 'display: none;' : ''}" />

ugly, but maybe working for you, via conditional style:

<h:component value="foo" style="#{!myBean.displayFoo ? 'display: none;' : ''}" />
短暂陪伴 2024-11-06 20:26:08

如果您的请求是数据库变量值的条件:

<c:if test="#{myObject.mycolumnTableDB==value1}">
   <!-- columns to display for this condition -->
</c:if>
<c:if test="#{myObject.mycolumnTableDB==value2}">
   <!-- other stuff to display -->
</c:if>

if you request is a conditional with the value of your variable of your database:

<c:if test="#{myObject.mycolumnTableDB==value1}">
   <!-- columns to display for this condition -->
</c:if>
<c:if test="#{myObject.mycolumnTableDB==value2}">
   <!-- other stuff to display -->
</c:if>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文