JSF:动态更改数据表单元格颜色 [英] JSF: Changing datatable cell color dynamically

查看:145
本文介绍了JSF:动态更改数据表单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,我希望根据对内容运行的某些分析来更改单元格的颜色。该表链接到一个Comment对象数组,我给出了一个String cssClass,它在运行分析后得到更新。这是我尝试插入数据表的rowClasses属性。它不起作用,我认为问题可能是我无法从数据表声明中访问为数据表的每一行创建的变量。

I have a datatable where I want to change the color of a cell based on some analysis that is run on the contents. The table is linked to an array of Comment objects, which I have given a String cssClass that gets updated once the analysis is run. This is what I have tried plugging into the rowClasses property of the datatable. It's not working and I think the issue may be that I cannot access the variable created for each row of the datatable, from inside the datatable declaration.

数据表代码:

<h:dataTable value="#{post.comments}" var="comment" class="hs-table" rowClasses="#{comment.cssClass}" >
             <h:column>
                   #{comment.name}
             </h:column>
             <h:column>
                   #{comment.email}
             </h:column>
             <h:column>
                   #{comment.msg}
             </h:column>
 </h:dataTable>

评论类:

public class Comment {
private String msg;
private String email;
private String name;
private Date date;
private String cssClass;

public Comment(){
    cssClass = "normColumn";
}
epublic String getCssClass() {
    return cssClass;
}

public void setCssClass(String cssClass) {
    this.cssClass = cssClass;
}

}

在托管bean中更新cssClass的位置:

Where the cssClass is updated in the managed bean:

if(tone>0)
            c.setCssClass("commentPos");
        else if(tone<0)
            c.setCssClass("commentNeg");

该类永远不会被分配。我做错了什么,或者这根本不可能?

The class never gets assigned. Am I doing something wrong, or is this simply not possible?

推荐答案

在标准JSF < h:dataTable> 组件,遗憾的是,不会按行计算 rowClasses 属性。它是基于每个表进行评估的。但是,像Tomahawk和PrimeFaces这样的组件库支持你在< t:dataTable> < p:中寻找的属性类型。 dataTable>

In the standard JSF <h:dataTable> component, the rowClasses attribute is unfortunately not evaluated on a per-row basis. It's evaluated on a per-table basis. Component libraries like Tomahawk and PrimeFaces however support the kind of attribute which you're looking for on their <t:dataTable> and <p:dataTable>.

使用标准JSF < h:dataTable> 组件需要提供以逗号分隔的所有行类的字符串。这看起来像这样:

With the standard JSF <h:dataTable> component you need to supply a comma-separated string of all row classes. This can look something like this:

public String getRowClasses() {
    StringBuilder rowClasses = new StringBuilder();

    for (Comment comment : comments) {
        if (rowClasses.length() > 0) rowClasses.append(",");
        rowClasses.append(comment.getCssClass());
    }

    return rowClasses.toString();
}

然后被引用为

<h:dataTable ... rowClasses="#{post.rowClasses}">



参见:




  • < h: dataTable> 标签文档 - 列出所有属性和可接受的值

  • See also:

    • <h:dataTable> tag documentation - lists all attributes and the accepted values
    • 这篇关于JSF:动态更改数据表单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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