setCellStyleGenerator - specific style according to cell value

Hi,

I’d like to set specific cell style of a Table according to cell value. Something like this: if cell value is ANOM1 I’d like to make the background red, if cell value is ANOM2 I’d like to make the background green. I don’t know how can I see cell value from a Table at this point:

table.setCellStyleGenerator(new Table.CellStyleGenerator() {
// ...
}

I am new to Vaadin.

Tnx a lot to all.
12175.jpg

Hi

You can handle this with css. Try something like this:

table.setCellStyleGenerator(new Table.CellStyleGenerator() {
		public String getStyle(Object itemId, Object propertyId) {
			int row = ((Integer) itemId).intValue();
			int col = Integer.parseInt((String) propertyId);

			if (table.getItem(row).getItemProperty(col).getValue.equals("ANOM1") {
				return "red"
			}
			return "normal";
		}
	});

In the css you can access it with:

.v-table .v-table-cell-content-red {
	background-color: red;
}

.v-table .v-table-cell-content-normal {
	background-color: green;
}

Hope it helps.

Greets

It works.
Tnx a lot.

Good example by E. Yildiz.

One tip: Rather than returning “normal” for your normal “do not do anything extra” case, you can return null.
So instead of this:

return "normal";

do this:

return null;

That is one less thing to remember and get right/wrong.

Vaadin noob here.

Where exactly is the css file that I’m supposed where I’m supposed to add these css declarations? I have no WEB-INF folder since I’m deploying the VaadinServlets programmatically to Jetty.


Book of Vaadin 7 about Themes


Book of Vaadin 7 Theme Creation

In case you use Vaadin 6:

Read this chapter

Can any1 help me get this to work in current vaadin 7.1.9 ??

I tryed E. Yildiz suggestion. But I get :

java.lang.NumberFormatException: null

for the line:

int col = Integer.parseInt((String) propertyId);

Since the posts is 2 years old, i am guessing that something change in recent versions.

Ohh, a little background maybe:
My goal is pretty much the same. Change the background color of individual cells based on content of that cell.

I should also mention that I am rather new to vaadin. (First (training) Project)

In the example above the assumption is made that all property id’s are numeric and represents the column index. That is not necessaraly always true. The property id’s can be any thing.

You can totally omit the line that is causing the exception and instead of
col
just pass
propertyId
to the getItemProperty. It should work equally well.

Thanks!

Had to encapsulate the if clause in the example in another if… checking if propertyId != null !

Not really sure why it can be null… but then again, i havent taken the time to read the docu or look at any source.

A CellStyleGenerator is called with a null propertyId for each row to allow styling entire rows, not just cells.

Hey Ranjit, have you read
Setting Individual Cell Styles
at
https://vaadin.com/book/-/page/components.table.html
?

If that doesn’t help, could you post runnable example code?