在 JSF 中动态更改 h:datatable 单元格颜色或样式 [英] Changing h:datatable cell color or style dynamically in JSF

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

问题描述

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

数据表代码:

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

评论类:

公共类注释{私人字符串味精;私人字符串电子邮件;私人字符串名称;私人日期;私人字符串 cssClass;公开评论(){cssClass = "normColumn";}epublic String getCssClass() {返回 cssClass;}公共无效 setCssClass(String cssClass) {this.cssClass = cssClass;}

}

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

if(tone>0)c.setCssClass("commentPos");否则如果(音<0)c.setCssClass("commentNeg");

该类永远不会被分配.是我做错了什么,还是根本不可能?

解决方案

在标准 JSF 组件中,很遗憾没有评估 rowClasses 属性以每行为基础.它是基于每个表进行评估的.然而,像 Tomahawk 和 PrimeFaces 这样的组件库支持您在它们的 <t:dataTable> 上寻找的那种属性.>

使用标准 JSF 组件,您需要提供所有行类的逗号分隔字符串.这看起来像这样:

public String getRowClasses() {StringBuilder rowClasses = new StringBuilder();对于(评论评论:评论){if (rowClasses.length() > 0) rowClasses.append(",");rowClasses.append(comment.getCssClass());}返回 rowClasses.toString();}

然后被引用为

另见:

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.

Datatable code:

<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>

The Comment class:

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;
}

}

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?

解决方案

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>.

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();
}

which is then to be referenced as

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

See also:

这篇关于在 JSF 中动态更改 h:datatable 单元格颜色或样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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