Changing h:datatable cell color or style dynamically in JSF

后端 未结 1 1915
灰色年华
灰色年华 2020-12-11 18:23

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

相关标签:
1条回答
  • 2020-12-11 19:06

    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:

    • <h:dataTable> tag documentation - lists all attributes and the accepted values
    0 讨论(0)
提交回复
热议问题