javavaadinvaadin-gridvaadin23

How to change row bg-color in Vaadin when condition is met?


I am using Vaadin v23 and I am trying to change bg-color of a row when certain condition is met, like below:

grid.setClassNameGenerator(row -> row.getRemoved() ? LumoUtility.Background.CONTRAST : "");

Why doesn't it work?

I tried to do it with custom .css file and it works, but I would prefer to use built-in CONTRAST style.

grid.setClassNameGenerator(row -> row.getRemoved() ? "grid-row-removed" : "";

Solution

  • The problem is that the CSS must be in the scope of the grid.

    So you must define the class in /frontend/themes/<your theme>/components/vaadin-grid.css

    Then you could do it like this:

    .grid-row-removed {
        background-color: var(--lumo-contrast);
    }