Generate a custom column related to the Entity inside the Grid.

Hi I want to ask about my problem in Grid :
I want the “stato” column be defined in new label for example if it is 0 = it will be named as “available” see the attachment

41009.png

I assume you are using Vaadin 8, in that case you could use lambda expression to convert the value in addColumn(…).

E.g. it could be something like

grid.addColumn(myBean -> (myBean.getStato() == 0 ? "available" : myBean.getStato().toString() ).setCaption("Availability");

Hi Tatu,

    Thanks for your Response! yes I am using vaadin. I tried that code but there is an error which is the getStato() is an int and cannot convert to string .

Hi Adrian,

Ah, it is not Integer. Then you need another way to convert the int to String, like this or something else that fits your purposes.

grid.addColumn(myBean -> (myBean.getStato() == 0 ? "available" : ""+myBean.getStato() ).setCaption("Availability");

thanks tatu!

One Last thing it is also related to this topic.
How about when I will add an Icon to it?

         grid.addColumn(myBean -> (myBean.getState()== 0 ? VaadinIcons.Check.getHtml(), ContentMode.HTML
                            : VaadinIcons.Check.getHtml(), ContentMode.HTML));

I do that code and there is an error
I try to not have getHtml and content mode but it will produce the code of the css.

If you want to use icon, yes VaadinIcons.Check.getHtml() produces good value for the String. Do not use content mode, instead add HTMLRenderer for the column.

Hi Tatu, Thanks but I have a problem when I use the Label as the Icon

// setting labels for icons
Label greenLabel = new Label();
greenLabel.addStyleName(“greenIconLabel”);
greenLabel.setIcon(VaadinIcons.CIRCLE);
Label redLabel = new Label();
redLabel.addStyleName(“redIconLabel”);
redLabel.setIcon(VaadinIcons.CIRCLE);

grid.addColumn(myBean → (myBean.getState()== 0 ? greenLabel .getHtml()
: redLabel.getHtml())).setRenderer(new HtmlRenderer());

// this has a problem and does not render at all

The label is not needed here. Just set the value with the lambda as in my example. If you need to style the colors, do not use label hack for that. Instead set style generator for those columns.

Thanks Tatu! :slight_smile: