Vaadin setCellStyleGenerator (v8) with custom Grid

I am trying to set a cell in a grid to a color based on the content of the cell. I have:

productGrid.setCellStyleGenerator(cellRef -> "tor".equals(cellRef.getPropertyId())? "tor" : null); and in the CSS:

.tor .v-grid-cell-content{
        background-color: #FF0000;
        }

The grid is of a custom data set, Grid and I am getting a message that setCellStyleGenerator is undefined for type Grid . I don’t understand what I need to do to get this working correctly and would appreciate any advice.

In V8, you should use Column.setStyleGenerator() for each of the relevant colums.

The thing is I don’t want to set an entire column, I just want to set the cells… I have approximately 15 different possibilities for cell colors depending on the item in the cell. It is probably easier to show than tell. The image was done in Qooxdoo and Javascript, but the program is being rewitten now to Vaadin and Java.
34104.png

Hi Gwen,

Do you set the same background color for the whole row or just the specific cell in one column? The CellStyleGeneration of The Vaadin 8 Grid works like this. You set a style generator for a column and for each cell in that column the style generation is executed against the item currently in question.

I made a test case using an enum to represent the different “states” I would have for my data. The enum also contained a textual description:

public enum TestEnum { FOO("This state is foo"), BAR("This state is bar"); private String textRepresentation; /* Constructor and a getter goes here */ } Then I’d create my column like this:

grid.addColumn(testBean -> testBean.getTestEnum().getTextRepresentation()) .setStyleGenerator(testBean -> testBean.getTestEnum().name().toLowerCase()) This will create a new column that shows the text representation in the column cell and uses
foo
and
bar
as style names.

If I’d apply this styling to all columns to get a consistent background color for all, I’d use:

grid.getColumns().forEach(col -> col.setStyleGenerator(/* Insert from above */)); Vaadin 8 gives you quite a lot of ways to manipulate and access the data, The whole object is at your disposal when you are generating the style names, all the information you might need is there for you.

If you still have some issues using the style name generation, I’m more than happy to help you out!

//Teemu

I am a newbie at Vaadin and Java…
I have a DAO to grab a list from a DB, and that puts it in a table, so I have:

productGrid.addColumn(ProductEvent::getProduct).setCaption(“Product”);
productGrid.addColumn(ProductEvent::getProductIssuanceTime).setCaption(“Issuance Date/Time”);

so I’m sorry, but you’ll have to give me just a little more to wrap my brain around. THANKS!!!

I have it working thanks!!

productGrid.addColumn(ProductEvent::getProduct)
.setCaption(“Product”)
.setStyleGenerator(productEvent → productEvent.getProduct()); // returns TOR, etc.
and then the css file contains the colors determined by the return…
Thanks!