【问题标题】:rendering values based on other column's values in primefaces datatable根据 primefaces 数据表中其他列的值呈现值
【发布时间】:2012-04-18 06:48:16
【问题描述】:

我正在primefaces的数据表中显示一些值。

这里是代码。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>Custom Rendering Data Table Test</title>
    </h:head>
    <h:body>
        <p:layout fullPage="true">
            <p:layoutUnit position="center">
                <p:dataTable var="car" value="#{tableBean.mediumCarsModel}" selection="#{tableBean.selectedCar}" 
                id="carList" paginator="true" rows="10" editable="true">  

        <f:facet name="header">  
            In-Cell Editing  
        </f:facet>

        <p:column selectionMode="multiple" style="width:18px" />  

        <p:column headerText="User Name" style="width:100px">
        <p:graphicImage value="C:\raman\AMAP\POC\primefaces\JSF2.0HelloWorld\WebContent\images\rssLogo.gif"/>    
        <p:graphicImage value="C:\raman\AMAP\POC\primefaces\JSF2.0HelloWorld\WebContent\images\System.gif"/>    
            <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{car.username}" />  
                </f:facet>  
                <f:facet name="input">  
                    <p:inputText value="#{car.username}" style="width:100%"/>  
                </f:facet>  
            </p:cellEditor>  
        </p:column> 


        <p:column headerText="Model" style="width:125px">  
            <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{car.model}" />  
                </f:facet>  
                <f:facet name="input">  
                    <p:inputText value="#{car.model}" style="width:100%"/>  
                </f:facet>  
            </p:cellEditor>  
        </p:column>  

        <p:column headerText="Year" style="width:125px">  
            <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{car.year}" />  
                </f:facet>  
                <f:facet name="input">  
                    <p:inputText value="#{car.year}" style="width:100%" label="Year"/>  
                </f:facet>  
            </p:cellEditor>  
        </p:column>  

        <p:column headerText="Manufacturer" style="width:125px">  
            <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{car.manufacturer}" />  
                </f:facet>  
                <f:facet name="input">  
                    <h:selectOneMenu value="#{car.manufacturer}" >  
                        <f:selectItems value="#{tableBean.manufacturers}"  
                            var="man"   
                            itemLabel="#{man}"  
                            itemValue="#{man}" />  
                    </h:selectOneMenu>  
                </f:facet>  
            </p:cellEditor>  
        </p:column>  

        <p:column headerText="Color" style="width:125px">  
            <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{car.color}" />  
                </f:facet>  
                <f:facet name="input">  
                    <h:selectOneMenu value="#{car.color}" >  
                        <f:selectItems value="#{tableBean.colors}"  
                            var="color"   
                            itemLabel="#{color}"  
                            itemValue="#{color}" />  
                    </h:selectOneMenu>  
                </f:facet>  
            </p:cellEditor>  
        </p:column>  

        <p:column headerText="Options" style="width:100px">  
            <p:rowEditor />  
        </p:column>  

    </p:dataTable>  
            </p:layoutUnit>
        </p:layout>                        
    </h:body>
</html>

现在,我必须读取年份的值,例如如果年份小于 1990 年,那么我必须显示 rsslogo 图像,否则显示系统图像。

请让我知道如何在 primefaces 中执行此操作...我是 primefaces 的初学者,仍处于学习阶段....

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    使用条件渲染:

    <p:graphicImage value="path/to/your/image1" rendered="#{car.year lt 1990}/>
    <p:graphicImage value="path/to/your/image2" rendered="#{car.year ge 1990}/>
    

    value 属性的内容替换为正确的路径(您不应使用问题中显示的绝对路径!!)。

    关键字lt 表示“小于”,ge 表示“大于或等于”。

    【讨论】:

    • 很高兴听到它有效。然后请将我的回答标记为已接受(请参阅我对您问题的评论)。
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多