File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes datatable - row colors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "datatable - row colors" Watch "datatable - row colors" New topic
Author

datatable - row colors

rakesh verma
Greenhorn

Joined: Jul 13, 2005
Posts: 22
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>
<hutputText value="#{item.name}" />
</h:column>
<h:column>
<hutputText 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
rakesh verma
Greenhorn

Joined: Jul 13, 2005
Posts: 22
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
<hutputText class="#{item.style}" value="#{item.name}" />
vishwa venkat
Ranch Hand

Joined: Nov 22, 2003
Posts: 185
One of the options I found is that you can have one color for even rows and another color of odd rows -

using the attribute rowClasses="list-row-even,list-row-odd" in datatable component.
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
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.
rakesh verma
Greenhorn

Joined: Jul 13, 2005
Posts: 22
Thanks for the reply Dusi.
We are using jsf ri and I don't see rowStyleClass in tld. Can I know which implementation you are using.
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
I am using Apache Tomahawk's t:datatable. I am not sure about Sun RI.
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
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(hutputText) 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.
rakesh verma
Greenhorn

Joined: Jul 13, 2005
Posts: 22
Thank you Dusi.
I am thinking of this option...
Susana Gurrea
Greenhorn

Joined: Sep 04, 2006
Posts: 1
hi i did a similary example, to print one column of a table in different colours...

<af:column headerText="#{bindings.ClientesView1.labels.Codigo}"
sortProperty="Codigo" sortable="true"
binding="#{backing_app_Clientes.column1}"
id="column1" formatType="text">
<afutputText value="#{row.Codigo}"
binding="#{backing_app_Clientes.outputText14}"
id="outputText14"
inlineStyle="color:#{row.tieneNotasdumm ? 'red' : 'black'};"/>
</af:column>

in my case i work with .jspx but i hope this can help you..
Master Blaster
Greenhorn

Joined: Sep 04, 2006
Posts: 3
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>
Radha Bajwa
Greenhorn

Joined: Jan 25, 2007
Posts: 4
Hello Rakesh and Master Blaster,

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;

else
return "redcolor"; }

Any help on this will be truly welcome.

Regards
Radha
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
Hello Radha,
We are doing something similar. He is my code....

in the jsp page.....



here tableOddRow and tableEvenRow are defined in the styleSheet.

in the backing bean,



hope this helps.....


-Rajani.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi,

Just a quick note t:column has a styleClass property, meaning that you can specify indiviudal styles e.g,




hope this helps,

John
[ January 29, 2007: Message edited by: John Bartlett ]
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Hi everyone, how are you?

I would like to color my dataGrid's rows, I set this way:




Where can I put the color?


André AS
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

In row classes CSS class, define the background property and set the color you want.
It will start taking effect. For example

HTH,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Prithvi Sehgal, worked!

thanks a lot...

now I am facing trouble applying CSS look at:




cabecalho is a CSS but does not apply for the grid's hearder...
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
André Asantos wrote:



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,

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: datatable - row colors
 
Similar Threads
Unable to display header in outer JSF datatable of a nested datatable using facet tag
Finding which cell was clicked in a DataTable?
JSF + RichFaces not able to reRender
How to use datatable with no records in a list
how to use dataTable without scrolling header