I am using datatable and each row in table should have different color according to a value of one of the proerty of row. Here it is <h:dataTable value="#{form.list}" var="item" id="table"> <h:column> <h:outputText value="#{item.name}" /> </h:column> <h:column> <h:outputText value="#{item.status}" /> </h:column> </h:dataTable> each row should be of a color depending on 'status' value of that row. Say, if it is pending - it should be red color, if it is approved - it should be green color......
Any advise is really appreciated. Please help me out, Thanks
I have a method in each row, which has style class to be used for that row color. I couldn't give rowclasses in datatable as follows, as it is giving null point er exception(may be its not instantiating item object) <h:dataTable value="#{form.list}" var="item" id="table" rowclasses="#{item.style}">
Even I couldn't specify class while displaying text. As it is just displaying background for the text but not for entire column <h:outputText class="#{item.style}" value="#{item.name}" />
Try to use rowStyleClass atrribute instead of rowClasses in your datatable. I used something like this in my code: rowStyleClass="#{myBackingBean.rowStyleClass}"
In my backing bean I have a String variable rowStyleClass and its corresponding getters and setters. In my getter I have code that reads the datatable row and returns the desired styleClass in the form of String.
I am not sure if this is a complete solution, but it is working for me.
If you just need the font to be in a different color, say red, for some rows, try using the 'style' attribute for the text(h:outputText) in each column(h:column). This style should be bound to a backing bean property that will return a String something like "color:red". You may still use the rowClasses attribute for datatable to get the alternate color effect.
Mr.Rakesh verma, You can try this, it works for me <h:dataTable rowClasses="#{managedbean.rowcolor}"> </h:dataTable>
In managedBean class public String getRowcolor() { if() //some condition on value of row return "redcolor";//Where redcolor is the styleclassname; else return "greencolor";
}
In Jsp , <style> .redcolor { background-color:red; } </style>
I am now in a similar situation as Rakesh wherein I need to show a different row color in the datatable based on a value in the data bean.
Here is how I wrote the code in the managed bean
public String getRowColor() { EventTypeSimple object = (EventTypeSimple)table1.getRowData(); //this line above returns a null - also note this object is bound to the values in the jsp
if(object.isModifiable()) //some condition on value of row return "greencolor";//Where redcolor is the styleclassname;
cabecalho is a CSS but does not apply for the grid's hearder...
Andre, i don't think it is the good way to apply CSS. It won't take the effect this way. As you have specified the
header class. Put an entry in your CSS file as
Remove the css you have put and it should work.
HTH,