I’m trying to style individual row in a Grid based on a value. According to Vaadin Docs, there are two ways to accomplish that: ComponentRenderer, TemplateRenderer. I’m using ComponentRenederer and its works just fine:
Grid<TabLine> grid = new Grid<>();
grid.setItems(getLines());
grid.addClassName("my");
grid.addColumn(new ComponentRenderer<>(tabline -> {
Label status = new Label(tabline.getStatus());
status.addClassName("style-"+tabline.getStatus()); // styling column based on a status
return status;
}
),"status").setHeader("Status");
But with a ComponentRenderer Sorting by this column is not working.
What should I do to make this column sortable? Should I use TemplateRenderer instead of ComponentRenderer?
If you use ComponentRenderer, by default toString() representation of the component is used for sorting, which may produce results that look random. So you need to for example add custom comparator as described in above doc.
Tatu Lund:
If you use ComponentRenderer, by default toString() representation of the component is used for sorting, which may produce results that look random. So you need to for example add custom comparator as described in above doc.
Thank you so much!
So I tried to use Comparator and it works :