动态更改 <h:dataTable> 中单元格的 CSS 样式柱子 [英] Dynamically change CSS style of cell in &lt;h:dataTable&gt; column

查看:20
本文介绍了动态更改 <h:dataTable> 中单元格的 CSS 样式柱子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算B列的单元格值以及如何动态更改其css样式

how to calculate cell values of B column and how to change their css style dynamically

我的Java对象:

public class MyObject{
   private Date date;
   private int A;
   private int C;

   //Getters & Setters
}

我的托管 bean:

public class MyBean(){
    List<MyObject> List = myObjectDao.FindAll();

    //Getters & Setters
}

我的jsf代码:

<p:dataTable id="idList" var="list" value="#{myBean.list}" >
    <p:column headerText="DATE">
        <h:outputText value="#{list.date}"  />
    </p:column>
        <p:column headerText="A">
        <h:outputText value="#{list.A}"  />
    </p:column>
        <p:column headerText="B">
        <h:outputText value="????????" style="???????"  //>
    </p:column>
        <p:column headerText="C">
        <h:outputText value="#{list.C} />
    </p:column>
</p:dataTable> 

推荐答案

你可以在 EL 中使用条件运算符 ?:.

You can just use the conditional operator ?: in EL.

例如

<c:set var="B" value="#{(list.A / list.C) * 100}" />
<h:outputText value="#{B}" style="display: block; background-color: #{B lt 50 ? 'red' : (B lt 90 ? 'blue' : 'green')}" />

如果 B 也用在模型或控制器的其他地方,那么你可以添加一个 public int getB() 方法,它只包含 return (A/C) * 100; 然后使用 #{list.B} 而不是 #{B}.

If B is also used elsewhere in the model or controller, then you could add a public int getB() method which just contains return (A/C) * 100; and then use #{list.B} instead of #{B}.

请注意,正确的设计是改用 CSS 类.例如

Note that proper design is to use a CSS class instead. E.g.

<h:outputText value="#{B}" styleClass="percentage #{B lt 50 ? 'poor' : (B lt 90 ? 'average' : 'good')}" />

td .percentage {
    display: block;
}

.percentage.poor {
    background-color: red;
}

.percentage.average {
    background-color: blue;
}

.percentage.good {
    background-color: green;
}

您当然也可以按照另一个答案的建议在 getter 方法中执行 CSS 样式/类的确定,但这种关注点分离很差.

You can of course also perform the determination of CSS style/class in a getter method as suggested by the other answer, but that's a poor separation of concerns.

这篇关于动态更改 <h:dataTable> 中单元格的 CSS 样式柱子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆