java 多个字段的条件显示 - JSF 2.0 / Primefaces
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5592248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
conditional display for multiple fields - JSF 2.0 / Primefaces
提问by Bogdan
What is the best strategy for conditionally displaying multiple elements (for example a list of fields that depends on a bean value)?
有条件地显示多个元素(例如依赖于 bean 值的字段列表)的最佳策略是什么?
Possible solutions I thought of:
我想到的可能解决方案:
- JSTL
<c:if ... >
clause. From what I understand using JSTL with JSF is discouraged - 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...
- Putting the elements on a container and setting the rendered attribute on the container
- JSTL
<c:if ... >
条款。据我了解,不鼓励在 JSF 中使用 JSTL - 使用大多数组件的“rendered”属性。不幸的是,当我必须处理很多字段时,将它们设置在每个字段上变得很笨拙......
- 将元素放在容器上并在容器上设置渲染属性
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...
选项 3 似乎是最明智的,但我不知道使用什么组件来包装这些字段。它需要是一个不影响布局的组件......
I could use a span as a wrapper and set a CSS visible property, but the fields will still be rendered, just invisible.
我可以使用 span 作为包装器并设置 CSS 可见属性,但字段仍将呈现,只是不可见。
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
.
这是一些实际的布局代码。我都试过<h:panelGroup>
& <ui:fragment>
。使用这些标签中的任何一个都会将我的所有字段<td>
放在一个panelGrid
.
The only thing that works the way I want is #2 from the list above.
唯一按照我想要的方式工作的是上面列表中的#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>
回答by Matthew Farwell
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.
唯一的小缺点是如果显示是动态的(您更改了支持 bean 的值),并且您需要重新显示容器,则它不存在。您需要将容器包装在另一个容器中,然后重新渲染。
回答by wjans
You can use h:panelGroup
as a container for this. It has a rendered property and will result in a span (by default).
您可以将其h:panelGroup
用作容器。它有一个渲染属性,将产生一个跨度(默认情况下)。
回答by LanceShaw
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.
我不得不求助于使用,因为当我将我的列值包装在另一个组件(如 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>
surprisingly, <c:otherwise>
didn't seem to work for me though.
令人惊讶的是,<c:otherwise>
虽然似乎对我不起作用。
回答by chris
ugly, but maybe working for you, via conditional style:
丑陋,但也许对你有用,通过条件样式:
<h:component value="foo" style="#{!myBean.displayFoo ? 'display: none;' : ''}" />
回答by Carlos Terán
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>