Formatting data in grid

Without any special configuration, Grid tries to find a Converter for converting the property value into a String that can be shown in the browser. The ConverterFactory configured for the session is used for this purpose. If no compatible converter is found, Grid will instead fall back to using toString() on the property value.

CellStyleGenerator

Grid does also provide a couple of mechanisms for fine-tuning how the data is displayed. The simplest way of controlling the data output is to use a CellStyleGenerator to add custom stylenames to individual cells, thus affecting which CSS rules from the theme are applied to each cell.

Java

  1. grid.setCellStyleGenerator(new CellStyleGenerator() {
  2. @Override
  3. public String getStyle(CellReference cellReference) {
  4. if ("amount".equals(cellReference.getPropertyId())) {
  5. Double value = (Double) cellReference.getValue();
  6. if (value.doubleValue() == Math.round(value.doubleValue())) {
  7. return "integer";
  8. }
  9. }
  10. return null;
  11. }
  12. });
  13. getPage().getStyles().add(".integer { color: blue; }");

We have not yet defined any Converter or Renderer in this example. This means that Grid will use a StringToDoubleConverter to convert the Double values from the data source into Strings that are sent to the browser and displayed in each cell.

To keep this example as simple as possible, we are dynamically injecting new CSS styles into the page. In a real application, it’s recommended to instead add the styles to the theme since that helps with separation of concerns.

Renderer

The next thing you can do to control how the data is displayed is to use a Renderer. The Renderer will receive the value from the data source property, possibly after it has been converted to the Renderer‘s input type using a Converter. The Renderer is will then make sure the value gets show in an appropriate way in the browser. A simple renderer might just show the data as text, but there are also more complex Renderers that e.g. show a numerical value as a progress bar.

Will will use a NumberRenderer using a currency format to render the cell values for the Amount column. To do this, we simply create and configure it and then set it as the Renderer for the designated column. No Converter will be used in this case since NumberRenderer already knows ho to handle values of the type Double.

Java

  1. NumberFormat poundformat = NumberFormat.getCurrencyInstance(Locale.UK);
  2. NumberRenderer poundRenderer = new NumberRenderer(poundformat);
  3. grid.getColumn("amount").setRenderer(poundRenderer);

Converter

The last way of controlling how data is displayed is to use a Converter. The framework will in most cases find and use a suitable Converter, but you can also supply your own if the default conversion is not what you need. The following example uses a custom Converter for the Count column to change the value into HTML strings with different HTML for even and odd counts. Grid will by default protect you from cross-site scripting vulnerabilities by not interpreting HTML in cell values. This can be overridden by setting the column to use a HtmlRenderer instead of the default TextRenderer. Both those renderers expect String values. Since we will not be editing any values, the Converter doesn’t need to support changing the HTML strings back into integers.

Java

  1. grid.getColumn("count").setConverter(new StringToIntegerConverter() {
  2. @Override
  3. public String convertToPresentation(Integer value, Class<? extends String> targetType, Locale locale)
  4. throws Converter.ConversionException {
  5. String stringRepresentation = super.convertToPresentation(value, targetType, locale);
  6. if (value.intValue() % 2 == 0) {
  7. return "<strong>" + stringRepresentation + "</strong>";
  8. } else {
  9. return "<em>" + stringRepresentation + "</em>";
  10. }
  11. }
  12. });
  13. grid.getColumn("count").setRenderer(new HtmlRenderer());

Full example

Putting all these pieces together, we end up with this class that uses the same data as in the Using Grid with a Container example.

Java

  1. import java.text.NumberFormat;
  2. import java.util.Locale;
  3. import com.vaadin.annotations.Theme;
  4. import com.vaadin.data.util.converter.Converter;
  5. import com.vaadin.data.util.converter.StringToIntegerConverter;
  6. import com.vaadin.server.VaadinRequest;
  7. import com.vaadin.ui.Grid;
  8. import com.vaadin.ui.Grid.CellReference;
  9. import com.vaadin.ui.Grid.CellStyleGenerator;
  10. import com.vaadin.ui.UI;
  11. import com.vaadin.ui.renderers.HtmlRenderer;
  12. import com.vaadin.ui.renderers.NumberRenderer;
  13. @Theme("valo")
  14. public class FormattingDataInGrid extends UI {
  15. @Override
  16. protected void init(VaadinRequest request) {
  17. Grid grid = new Grid(GridExampleHelper.createContainer());
  18. setContent(grid);
  19. grid.setCellStyleGenerator(new CellStyleGenerator() {
  20. @Override
  21. public String getStyle(CellReference cellReference) {
  22. if ("amount".equals(cellReference.getPropertyId())) {
  23. Double value = (Double) cellReference.getValue();
  24. if (value.doubleValue() == Math.round(value.doubleValue())) {
  25. return "integer";
  26. }
  27. }
  28. return null;
  29. }
  30. });
  31. getPage().getStyles().add(".integer { color: blue; }");
  32. NumberFormat poundformat = NumberFormat.getCurrencyInstance(Locale.UK);
  33. NumberRenderer poundRenderer = new NumberRenderer(poundformat);
  34. grid.getColumn("amount").setRenderer(poundRenderer);
  35. grid.getColumn("count").setConverter(new StringToIntegerConverter() {
  36. @Override
  37. public String convertToPresentation(Integer value,
  38. Class<? extends String> targetType, Locale locale)
  39. throws Converter.ConversionException {
  40. String stringRepresentation = super.convertToPresentation(
  41. value, targetType, locale);
  42. if (value.intValue() % 2 == 0) {
  43. return "<strong>" + stringRepresentation + "</strong>";
  44. } else {
  45. return "<em>" + stringRepresentation + "</em>";
  46. }
  47. }
  48. });
  49. grid.getColumn("count").setRenderer(new HtmlRenderer());
  50. }
  51. }