How to dynamically add an icon to a Grid Cell. For example, i have a column that will display either True or False. I want to display some icon if the value is True/False.
Can someone please help me on this?
How to dynamically add an icon to a Grid Cell. For example, i have a column that will display either True or False. I want to display some icon if the value is True/False.
Can someone please help me on this?
What you’re looking for is the method [grid.addComponentColumn()]
(https://vaadin.com/components/vaadin-grid/java-examples/using-components). Inside that component renderer you can check whatever value and return a different component depending on that value.
Grid<Person> grid = new Grid<>();
grid.addComponentColumn(item -> {
Icon icon;
if(item.isAdult()){
icon = VaadinIcon.CHECK_CIRCLE.create();
icon.setColor("green");
} else {
icon = VaadinIcon.CLOSE_CIRCLE.create();
icon.setColor("red");
}
return icon;
})
.setKey("adult")
.setHeader("Adult")
.setComparator(Comparator.comparing(Person::isAdult));
I have exactly this setup for a boolean column (except that I don’t use Person class), so I left some minor improvements like icon-color and comparator here
Great. It works. Thank You!
Great!