You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FormattingDataInGrid.asciidoc 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ---
  2. title: Formatting Data In Grid
  3. order: 24
  4. layout: page
  5. ---
  6. [[formatting-data-in-grid]]
  7. = Formatting data in grid
  8. Without any special configuration, Grid tries to find a `Converter` for
  9. converting the property value into a String that can be shown in the
  10. browser. The `ConverterFactory` configured for the session is used for
  11. this purpose. If no compatible converter is found, Grid will instead
  12. fall back to using `toString()` on the property value.
  13. [[cellstylegenerator]]
  14. CellStyleGenerator
  15. ^^^^^^^^^^^^^^^^^^
  16. Grid does also provide a couple of mechanisms for fine-tuning how the
  17. data is displayed. The simplest way of controlling the data output is to
  18. use a `CellStyleGenerator` to add custom stylenames to individual cells,
  19. thus affecting which CSS rules from the theme are applied to each cell.
  20. [source,java]
  21. ....
  22. grid.setCellStyleGenerator(new CellStyleGenerator() {
  23. @Override
  24. public String getStyle(CellReference cellReference) {
  25. if ("amount".equals(cellReference.getPropertyId())) {
  26. Double value = (Double) cellReference.getValue();
  27. if (value.doubleValue() == Math.round(value.doubleValue())) {
  28. return "integer";
  29. }
  30. }
  31. return null;
  32. }
  33. });
  34. getPage().getStyles().add(".integer { color: blue; }");
  35. ....
  36. We have not yet defined any `Converter` or `Renderer` in this example. This
  37. means that Grid will use a `StringToDoubleConverter` to convert the Double
  38. values from the data source into Strings that are sent to the browser
  39. and displayed in each cell.
  40. To keep this example as simple as possible, we are dynamically injecting
  41. new CSS styles into the page. In a real application, it's recommended to
  42. instead add the styles to the theme since that helps with separation of
  43. concerns.
  44. [[renderer]]
  45. Renderer
  46. ^^^^^^^^
  47. The next thing you can do to control how the data is displayed is to use
  48. a `Renderer`. The `Renderer` will receive the value from the data source
  49. property, possibly after it has been converted to the `Renderer`{empty}'s input
  50. type using a `Converter`. The `Renderer` is will then make sure the value
  51. gets show in an appropriate way in the browser. A simple renderer might
  52. just show the data as text, but there are also more complex `Renderer`{empty}s
  53. that e.g. show a numerical value as a progress bar.
  54. Will will use a `NumberRenderer` using a currency format to render the
  55. cell values for the `Amount` column. To do this, we simply create and
  56. configure it and then set it as the `Renderer` for the designated column.
  57. No `Converter` will be used in this case since `NumberRenderer` already
  58. knows ho to handle values of the type Double.
  59. [source,java]
  60. ....
  61. NumberFormat poundformat = NumberFormat.getCurrencyInstance(Locale.UK);
  62. NumberRenderer poundRenderer = new NumberRenderer(poundformat);
  63. grid.getColumn("amount").setRenderer(poundRenderer);
  64. ....
  65. [[converter]]
  66. Converter
  67. ^^^^^^^^^
  68. The last way of controlling how data is displayed is to use a `Converter`.
  69. The framework will in most cases find and use a suitable `Converter`, but
  70. you can also supply your own if the default conversion is not what you
  71. need. The following example uses a custom `Converter` for the `Count` column
  72. to change the value into HTML strings with different HTML for even and
  73. odd counts. Grid will by default protect you from cross-site scripting
  74. vulnerabilities by not interpreting HTML in cell values. This can be
  75. overridden by setting the column to use a `HtmlRenderer` instead of the
  76. default `TextRenderer`. Both those renderers expect String values. Since
  77. we will not be editing any values, the Converter doesn't need to support
  78. changing the HTML strings back into integers.
  79. [source,java]
  80. ....
  81. grid.getColumn("count").setConverter(new StringToIntegerConverter() {
  82. @Override
  83. public String convertToPresentation(Integer value, Class<? extends String> targetType, Locale locale)
  84. throws Converter.ConversionException {
  85. String stringRepresentation = super.convertToPresentation(value, targetType, locale);
  86. if (value.intValue() % 2 == 0) {
  87. return "<strong>" + stringRepresentation + "</strong>";
  88. } else {
  89. return "<em>" + stringRepresentation + "</em>";
  90. }
  91. }
  92. });
  93. grid.getColumn("count").setRenderer(new HtmlRenderer());
  94. ....
  95. [[full-example]]
  96. Full example
  97. ^^^^^^^^^^^^
  98. Putting all these pieces together, we end up with this class that uses
  99. the same data as in the <<UsingGridWithAContainer#using-with-a-container,Using
  100. Grid with a Container>> example.
  101. [source,java]
  102. ....
  103. import java.text.NumberFormat;
  104. import java.util.Locale;
  105. import com.vaadin.annotations.Theme;
  106. import com.vaadin.data.util.converter.Converter;
  107. import com.vaadin.data.util.converter.StringToIntegerConverter;
  108. import com.vaadin.server.VaadinRequest;
  109. import com.vaadin.ui.Grid;
  110. import com.vaadin.ui.Grid.CellReference;
  111. import com.vaadin.ui.Grid.CellStyleGenerator;
  112. import com.vaadin.ui.UI;
  113. import com.vaadin.ui.renderers.HtmlRenderer;
  114. import com.vaadin.ui.renderers.NumberRenderer;
  115. @Theme("valo")
  116. public class FormattingDataInGrid extends UI {
  117. @Override
  118. protected void init(VaadinRequest request) {
  119. Grid grid = new Grid(GridExampleHelper.createContainer());
  120. setContent(grid);
  121. grid.setCellStyleGenerator(new CellStyleGenerator() {
  122. @Override
  123. public String getStyle(CellReference cellReference) {
  124. if ("amount".equals(cellReference.getPropertyId())) {
  125. Double value = (Double) cellReference.getValue();
  126. if (value.doubleValue() == Math.round(value.doubleValue())) {
  127. return "integer";
  128. }
  129. }
  130. return null;
  131. }
  132. });
  133. getPage().getStyles().add(".integer { color: blue; }");
  134. NumberFormat poundformat = NumberFormat.getCurrencyInstance(Locale.UK);
  135. NumberRenderer poundRenderer = new NumberRenderer(poundformat);
  136. grid.getColumn("amount").setRenderer(poundRenderer);
  137. grid.getColumn("count").setConverter(new StringToIntegerConverter() {
  138. @Override
  139. public String convertToPresentation(Integer value,
  140. Class<? extends String> targetType, Locale locale)
  141. throws Converter.ConversionException {
  142. String stringRepresentation = super.convertToPresentation(
  143. value, targetType, locale);
  144. if (value.intValue() % 2 == 0) {
  145. return "<strong>" + stringRepresentation + "</strong>";
  146. } else {
  147. return "<em>" + stringRepresentation + "</em>";
  148. }
  149. }
  150. });
  151. grid.getColumn("count").setRenderer(new HtmlRenderer());
  152. }
  153. }
  154. ....