在JSF中动态更改h:datatable单元格的颜色或样式 [英] Changing h:datatable cell color or style dynamically in 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>
标记文档-列表所有属性和可接受的值 <h:dataTable>
tag documentation - lists all attributes and the accepted values
See also:
这篇关于在JSF中动态更改h:datatable单元格的颜色或样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!