Vaadin 14 Grid. Set style for content in cell, base on cell class

B"H
Hi
I dont found how to use the cell style to set a class to internal content.
e.g.
my css:

[part~="cell"]
[class~="rowRed"]
 ::slotted(vaadin-grid-cell-content) .spml-value {...}

and the dom is:

<vaadin-grid-cell-content slot="vaadin-grid-cell-content-8">
	<flow-component-renderer appid="ROOT" style="">
		<div class="spml-label spml-value" id="address/Countries">
			<span>United States</span>
			<j-tooltip><template></template></j-tooltip>
		</div></flow-component-renderer>
</vaadin-grid-cell-content>

Thank a lot.

Assuming you have given your cells classnames by calling setClassNameGenerator() on a given Column, then styling the cells would require two things:

First, create a custom CSS custom style themeFor = "vaadin-grid" (if one doesn’t already exist)

@CssImport(value = "./vaadin-grid-styles.css", themeFor = "vaadin-grid")

Then in vaadin-grid-styles.css (which should be located under the frontend directory of your project), you should do something like:

.rowRed {
	color:red;
}

This more complete example might be helpful: https://cookbook.vaadin.com/dynamic-grid-cell-background-color

B"H

Hi

Thanks

i did the CssImport and: [part~=“cell”]
[class~=“rowRed”]
::slotted(vaadin-grid-cell-content)

this does push the style. just that in .spml-value which is second level nested already have a style that wins.

despite try !important with no success, i tried to add the .spml-value to get higher resolution and maybe win the color.

Just that not sure this is possible at all, and not sure that the addition .spml-value is legal as currently it is not read into browser.

I’ve spent at least 3 hours trying to figure out how to change the background of a grid. I didn’t include the css file because I’ve tried about 10 different things. Also, why can’t you just use setClassName?

public class SetupPipelinesView extends ViewFrame implements AfterNavigationObserver {

    @CssImport(value="./styles/views/upload-grid.css", themeFor="vaadin-grid")
    private class PipelineDetails {
	
	   attachmentGrid.addComponentColumn(item -> createRemoveButton(attachmentGrid, item)).setFlexGrow(0).setWidth("55px").setClassNameGenerator(attachment -> "background");
	   attachmentGrid.addComponentColumn(AttachmentComponent::new).setTextAlign(ColumnTextAlign.START).setClassNameGenerator(attachment -> "background");

18456337.png

If you want to set a color to a cell then your code looks good (I think).

Here you have two examples:

As far as I can tell, I’ve effectively duplicated the code contained in the Vaadin Cookbook links listed above. For whatever reason, it doesn’t work. I’ve also tried the code contained in the Vaadin documentation [https://vaadin.com/components/vaadin-grid/java-examples/styling]
(https://vaadin.com/components/vaadin-grid/java-examples/styling). It doesn’t seem to work either.

Also, can you use the setClassName method (which is what I always used in Vaadin 8) instead of the setClassNameGenerator method?

.background {
    background-color: var(--lumo-primary-color-10pct);
}
public class SetupPipelinesView extends ViewFrame implements AfterNavigationObserver {

    @CssImport(value="./styles/views/upload-grid.css", themeFor="vaadin-grid")
    private class PipelineDetails {
	
	   attachmentGrid.addComponentColumn(item -> createRemoveButton(attachmentGrid, item)).setFlexGrow(0).setWidth("55px").setClassNameGenerator(e -> "background");
	   attachmentGrid.addComponentColumn(AttachmentComponent::new).setTextAlign(ColumnTextAlign.START).setClassNameGenerator(e -> "background");
	   
	   attachmentGrid.setClassNameGenerator(e -> "background")

Hi,
I think your main problem is the place of the annotation.
I can’t find anything in the documentation but I think that the CssImport should be added on top of Component:

@CssImport(value="./styles/views/upload-grid.css", themeFor="vaadin-grid")
public class SetupPipelinesView extends ViewFrame implements AfterNavigationObserver {

    private class PipelineDetails {

If you want to set the background of the color that does not depend on the content of the row then setClassName is the right Java method:
For example this file upload-grid.css:

:host {
    background-color: red;
}

[part~="cell"]
 {
    background-color: red;
}

:host(.background-example) {
    background-color: pink;
}

:host(.background-example) [part~="cell"]
 {
    background-color: pink;
}

With this css imported with CssImport, all the grid will have the background color red and the grid with grid.setClassName("background-example"); in pink.

You can find more information about how to style components here: https://vaadin.com/docs/v14/themes/styling-components.html#supported-css-selectors

For example:

[part~="cell"]
 {
  /* Styles that affect all grid cells, including header, body and footer cells */
}

[part~="body-cell"]
 {
  /* Styles that only affect all body cells */
}

But I agree that the theming inside a webcomponent can be pretty annoying.

Hi Jean-Christophe
I still can’t change the cell background color. I can change the grid background color, but that seems to be pointless if the cell color overrides the grid color. I’ve tried the import annotation on the outer class, the inner class, and the inner and outer. The cells are still color: var(–lumo-body-text-color).

:host(.upload-grid) {
    background-color: var(--lumo-primary-color-10pct);
}

:host(.upload-grid) [part~="cell"]
 {
    background-color: var(--lumo-primary-color-10pct);
}
 attachmentGrid.addComponentColumn(item -> createRemoveButton(attachmentGrid, item)).setFlexGrow(0).setWidth("55px").setClassNameGenerator(e -> "upload-grid");
            attachmentGrid.addComponentColumn(AttachmentComponent::new).setTextAlign(ColumnTextAlign.START).setClassNameGenerator(e -> "upload-grid");
            attachmentGrid.setClassName("upload-grid");

Hi Brian, this last styling you shared isn’t the correct way to style grid cells selectively. Could you try:

.upload-grid {
    background-color: var(--lumo-primary-color-10pct);
}

Hi,

I don’t understand why it’s not working.
I’ve got the same code and the background is correct.

/* Set the background of the grid if the grid has .upload-grid css class */
:host(.upload-grid) {
    background-color: var(--lumo-primary-color-10pct);
}
/* Set the background of the cell if the grid has .upload-grid css class */
:host(.upload-grid) [part~="cell"]
 {
    background-color: var(--lumo-primary-color-10pct);
}
/* Set the background of the cell if the cell has upload-grid css class */
[part~="cell"]
.upload-grid {
  background-color: var(--lumo-primary-color-10pct);
}

And here one example of a simple view:


@CssImport(value="./styles/upload-grid.css", themeFor="vaadin-grid")
@Route("bug")
public class BugView extends VerticalLayout {

    private Grid<MessageBean> messageBeanGrid;

    public BugView() {
        messageBeanGrid = new Grid<>();
        messageBeanGrid.setItems(createMessages());
        messageBeanGrid.addComponentColumn(item -> new Button(item.getMessage())).setFlexGrow(0).setWidth("55px")
            .setClassNameGenerator(e -> "upload-grid");
        messageBeanGrid.addComponentColumn(item -> new Button(item.getMessage())).setTextAlign(ColumnTextAlign.START)
            .setClassNameGenerator(e -> "upload-grid");
        add(new RouterLink("navigate",BugView.class));
        messageBeanGrid.setClassName("upload-grid");
        messageBeanGrid.setSizeFull();
        setSizeFull();
        add(messageBeanGrid);
    }

    private List<MessageBean> createMessages() {
        return IntStream.range(0, 10).mapToObj(i -> new MessageBean()).collect(Collectors.toList());
    }

    public class MessageBean {

        private String message;
        private String time;

        public String getMessage() {
            return "Button was clicked at " + LocalTime.now() + message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }
    }
}

I’ve attached a screenshot of the inspector, Perhaps you have css that overrides the default behaviour? (the default cell background color should be --lumo-base-color not --lumo-body-text-color)
18459506.png

I just noticed that shared-styles.js contains

 :host ::slotted(vaadin-grid) {
        max-width: calc(964px + var(--lumo-space-m));
        background-color: var(--lumo-base-color);
      }

Does that override what’s in my upload-grid.css?

No it shouldn’t (It doesn’t change my tests).

You can also try to remove the content of shared-styles.js and check the result. It may override your css.