I have a JSF page created from a template where I want to add CSS styles from my backing bean, because some CSS attributes (like color, etc.) are read from a configuration database. The colors are used for rowStyleClass in a p:dataTable via #{bean.getRowStyleClass(item)}. This works only if the style tag is hard-coded within the page. If the style tag is retrieved from the backing bean, the returned string is just printed on the page as normal string.
Kukeltje mentioned in Primefaces datatable: set row color from bean that the bean could create the selectors dynamically with return ".redBackgroundColor { background-color: #FF0000; !important; }" and that the bean could create the full css part (which I do in #{bean.styles}) but this results also in a dataTable without colored rows.
template.xhtml
<html>
<h:head>...</h:head>
<h:body>
<ui:insert name="styles"/>
<ui:insert name="content"/>
</h:body>
</html>
page.xhtml
<html>
<ui:composition template="/WEB-INF/template.xhtml">
<ui:define name="styles">
<!-- this works as expected -->
<style type="text/css">
.redBackgroundColor { background-color: #FF0000; !important; }
.blueBackgroundColor { background-color: #00FF00; !important; }
.greenBackgroundColor { background-color: #0000FF; !important; }
</style>
<!-- this does not work - it prints the string on the page -->
#{bean.styles}
</ui:define>
<ui:define name="content">
<!-- here comes the page content -->
<p:dataTable var="item"
value={#bean.values}
rowStyleClass="#{bean.getRowStyleClass(item)}">
...
</p:dataTable>
</ui:define>
</ui:composition>
</html>
Bean.java
public class Bean {
public String getStyles() {
return new StringBuffer()
.append("<style type=\"text/css\">")
.append(".redBackgroundColor { background-color: #FF0000; !important; }")
.append(".blueBackgroundColor { background-color: #00FF00; !important; }")
.append(".greenBackgroundColor { background-color: #0000FF; !important; }")
.toString();
}
public String getRowStyleClass(Item item) {
if (condition1) {
return "redBackgroundColor";
}
if (condition2) {
return "blueBackgroundColor";
}
if (condition3) {
return "greenBackgroundColor";
}
return null;
}
}
So, how can I create the styles in my backing bean and use them in the p:dataTable rowStyleClass attribute?
I am using Primefaces 6.0 on Wildfly 10.0.0.Final
Any hints welcome - Thank you!