本文介绍了如何有条件地渲染f:selectItem标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为<f:selectItem>标签指定条件渲染.我需要根据特定用户的状态显示<f:selectItem>选项.

How can I specify a conditional rendering for an <f:selectItem> tag.I need to display <f:selectItem> options according to a specific user's status.

例如,我想要类似的东西:

For example, I wanted something like:

<f:selectItem itemLabel="Yes! I need a girlfriend!"
             rendered="false(or some boolean condition)"
             itemValue="o1"/>

推荐答案

<f:selectItem> 不支持rendered属性.最接近的下注是itemDisabled属性,该属性仍显示该项目,但使该项目不可选择.在<f:selectItems>中也支持.

The <f:selectItem> does not support the rendered attribute. Your closest bet is the itemDisabled attribute which still displays the item, but makes it unselectable. This is also supported in <f:selectItems>.

如果是<p:selectOneMenu>,则只需添加一些CSS即可隐藏禁用的项目.

In case of <p:selectOneMenu> you can then just add some CSS to hide disabled items.

<p:selectOneMenu ... panelStyleClass="hideDisabled">
    <f:selectItem itemValue="1" itemLabel="one" />
    <f:selectItem itemValue="2" itemLabel="two" itemDisabled="#{some.condition}" />
    <f:selectItem itemValue="3" itemLabel="three" />
</p:selectOneMenu>
.ui-selectonemenu-panel.hideDisabled .ui-selectonemenu-item.ui-state-disabled {
    display: none;
}

对于<h:selectOneMenu>,您更依赖于Web浏览器是否支持通过CSS隐藏禁用的选项:

In case of <h:selectOneMenu> you're more dependent on whether the webbrowser supports hiding the disabled options via CSS:

<h:selectOneMenu ... styleClass="hideDisabled">
    <f:selectItem itemValue="1" itemLabel="one" />
    <f:selectItem itemValue="2" itemLabel="two" itemDisabled="#{some.condition}" />
    <f:selectItem itemValue="3" itemLabel="three" />
</h:selectOneMenu>
select.hideDisabled option[disabled] {
    display: none;
}

服务器端的替代方法是在单个<f:selectItem>周围引入JSTL <c:if>,以有条件地将其添加到这样的视图中(确保您了解JSTL在JSF中的工作方式:JSF2 Facelets中的JSTL ...有意义吗?):

The server side alternative is to bring in a JSTL <c:if> around the individual <f:selectItem> to contitionally add it to the view like this (make sure you're aware of how JSTL works in JSF: JSTL in JSF2 Facelets... makes sense?):

<f:selectItem itemValue="1" itemLabel="one" />
<c:if test="#{not some.condition}">
    <f:selectItem itemValue="2" itemLabel="two"  />
</c:if>
<f:selectItem itemValue="3" itemLabel="three" />

或者,您可以根据计算的条件简单地在后备bean中动态填充List<SelectItem>并将其与<f:selectItems>绑定.

Or, you could simply dynamically populate a List<SelectItem> in the backing bean based on the calculated conditions and bind it with <f:selectItems>.

这篇关于如何有条件地渲染f:selectItem标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 03:45