How to add a specific color to a table row in Vaadin?

I am trying to change the color of a row in a table based on the hex value for a row element. I am basically trying to generate css on the fly, similar to creating a cssLayout like this

CssLayout content = new CssLayout( ) { @Override public String getCss( Component c ) { return "background: " + colorCode + ";"; } }; 

Here is the code I'm using now

  table.setCellStyleGenerator( new Table.CellStyleGenerator( ) { public String getStyle( Object itemId, Object propertyId ) { return "green"; } } ); 

But it only works to set the style name, so I need to have millions of style names to accommodate all the possible hexadecimal values ​​for the colors that the user wants.

+4
source share
2 answers

You can use the CSSInject add-on to add the required style name on the fly.

See https://vaadin.com/directory#addon/cssinject

 String color = "#CCDDFF"; CSSInject css = new CSSInject(getUI()); css.setStyles("."+color+" { background-color: "+color+"; }"); 
+2
source

In an existing CSS file, you can define color changes as background-color: #00ff00; for the green style and return the green style to your limits. See color changes .

0
source

Source: https://habr.com/ru/post/1480721/


All Articles