【问题标题】:Render an icon conditionally有条件地渲染图标
【发布时间】:2019-03-24 10:40:48
【问题描述】:

在 Primefaces 的数据表中,我想有条件地在某个列中插入一个图标。

此列有两个值:1 或 0,如果为 1,则使用复选图标,如果为 0,则使用关闭图标。

我已经尝试过使用“rendered”、“style”、styleClass,但它对我不起作用。要么输出读取的值,要么不呈现任何内容(列为空)。

有什么办法吗?

我传递了一段代码:

<p:column headerText="Vota" width="30" filterBy="#{vot.estado}">
    <h:outputText value="#{vot.estado}" style="float:right #{vot.estado == 1 ? 'ui-icon-check' : 'ui-icon-close'}"/>
</p:column>

【问题讨论】:

  • ui-icon-checkui-icon-close 是样式类。像你一样在 style 属性中添加它是没有功能的,把它放在 styleClass 中(你可能需要添加一些其他的类,在浏览器开发工具中)

标签: jsf primefaces


【解决方案1】:

您在 style 属性中有图标信息,而它应该在 styleClass 属性中,并且您也需要通用 ui-icon 类。所以将您的代码更改为以下代码以使其工作

<p:column headerText="Vota" width="30" filterBy="#{vot.estado}">
    <h:outputText value="#{vot.estado}" style="float:right" styleClass="ui-icon #{vot.estado == 1 ? 'ui-icon-check' : 'ui-icon-close'}"/>
</p:column>

【讨论】:

    【解决方案2】:

    您可以将 rendered 属性与 outputText 本身一起使用,如下所示:

    <p:column headerText="Vota" width="30" filterBy="#{vot.estado}">
         <h:outputText rendered="#{vot.estado == 1}" value="#{vot.estado}" style="float:right" styleClass="ui-icon-check"/>
         <h:outputText rendered="#{vot.estado != 1}" value="#{vot.estado}" style="float:right" styleClass="ui-icon-close"/>
    </p:column>
    

    【讨论】:

    • 你甚至可以把它放在一个h:outputtext 中,就像在问题中已经尝试过的一样,但是使用 styleclass 而不是 style...
    【解决方案3】:
    <p:column headerText="Vota" width="30" filterBy="#{vot.estado}>
         <h:outputText value="#{vot.estado}"/>
         <i class="ui-icon #{vot.estado == 1 ? 'ui-icon-check' : 'ui-icon-close'}" style="display: inline-block"/>
    </p:column>
    

    【讨论】:

    • 为什么这么冗长?
    • 很大的改进。尝试直接在h:outputText上添加样式类?
    • 也许,但保持文本和图标分开不是更易读吗?
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2014-11-26
    • 2012-11-20
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    相关资源
    最近更新 更多