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.

components-grid.asciidoc 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. ---
  2. title: Grid
  3. order: 24
  4. layout: page
  5. ---
  6. [[components.grid]]
  7. = [classname]#Grid#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/grids-and-trees/grid"]
  11. endif::web[]
  12. ((("[classname]#Grid#")))
  13. [classname]#Grid# is many things, and perhaps the most versatile and powerful
  14. component in Vaadin. Like [classname]#Table#, it allows presenting and editing
  15. tabular data, but escapes many of [classname]#Table#'s limitations. Efficient
  16. lazy loading of data while scrolling greatly improves performance. Grid is
  17. scalable, mobile friendly, and extensible.
  18. [[components.grid.overview]]
  19. == Overview
  20. [classname]#Grid# is for displaying and editing tabular data laid out in rows
  21. and columns. At the top, a __header__ can be shown, and a __footer__ at the
  22. bottom. In addition to plain text, the header and footer can contain HTML and
  23. components. Having components in the header allows implementing filtering
  24. easily. The grid data can be sorted by clicking on a column header;
  25. shift-clicking a column header enables secondary sorting criteria.
  26. [[figure.components.grid.features]]
  27. .A [classname]#Grid#
  28. image::img/grid-features.png[width=70%, scaledwidth=100%]
  29. The data area can be scrolled both vertically and horizontally. The leftmost
  30. columns can be frozen, so that they are never scrolled out of the view. The data
  31. is loaded lazily from the server, so that only the visible data is loaded. The
  32. smart lazy loading functionality gives excellent user experience even with low
  33. bandwidth, such as mobile devices.
  34. The grid data can be edited with a row-based editor after double-clicking a row.
  35. The fields are generated with a field factory, or set explicitly, and bound to
  36. data with a field group.
  37. Grid is fully themeable with CSS and style names can be set for all grid
  38. elements. For data rows and cells, the styles can be generated with a row or
  39. cell style generator.
  40. Finally, [classname]#Grid# is designed to be extensible and used just as well
  41. for client-side development - its GWT API is nearly identical to the server-side
  42. API, including data binding.
  43. [[components.grid.overview.table]]
  44. === Differences to Table
  45. In addition to core features listed above, [classname]#Grid# has the following
  46. API-level and functional differences to Table:
  47. * Grid is not a [interfacename]#Container# itself, even though it can be bound to a container data source. Consequently, columns are defined differently, and so forth.
  48. * Rows can be added with [methodname]#addRow()# shorthand (during initialization) instead of [methodname]#addItem()#.
  49. * Use [methodname]#setHeightByRows()# and [methodname]#setHeightMode()# instead of [methodname]#setPageLength()# to set the height in number of rows.
  50. * Grid does not extend [classname]#AbstractSelect# and is not a field, but has its own selection API. [methodname]#addSelectionListener()# is called to define a [interfacename]#SelectionListener#. The listener also receives a collection of deselected items.
  51. * Grid does not support having all cells in editable mode, it only supports row-based editing, with a row mini-editor that allows saving or discarding the changes.
  52. * Grid has no generated columns. Instead, the container data source can be wrapped around a [classname]#GeneratedPropertyContainer#.
  53. * No column icons; you can implement them in a column with an [classname]#ImageRenderer#.
  54. * Components can not be shown in Grid cells; instead the much more efficient renderers can be used for the most common cases, and row editor for editing values.
  55. * Limited support for drag and drop: the user can drag columns to reorder them.
  56. In addition, Grid has the following visual changes:
  57. * Multiple selection is indicated with check boxes in addition to highlighting.
  58. * Grid does not show the row loading indicator like Table does.
  59. [[components.grid.data]]
  60. == Binding to Data
  61. [classname]#Grid# is normally used by binding it to a container data source,
  62. described in
  63. <<dummy/../../../framework/datamodel/datamodel-container#datamodel.container,"Collecting
  64. Items in Containers">>. The container must implement
  65. [interfacename]#Container.Indexed# interface. By default, it is bound to an
  66. [classname]#IndexedContainer#; Grid offers some shorthand methods to operate on
  67. the default container, as described later.
  68. You can set the container in the constructor or with
  69. [methodname]#setContainerDataSource()#.
  70. For example, if you have a collection of beans, you could wrap them in a Vaadin
  71. [classname]#BeanContainer# or [classname]#BeanItemContainer#, and bind to a [classname]#Grid# as follows
  72. [source, java]
  73. ----
  74. // Have some data
  75. Collection<Person> people = Lists.newArrayList(
  76. new Person("Nicolaus Copernicus", 1543),
  77. new Person("Galileo Galilei", 1564),
  78. new Person("Johannes Kepler", 1571));
  79. // Have a container of some type to contain the data
  80. BeanItemContainer<Person> container =
  81. new BeanItemContainer<Person>(Person.class, people);
  82. // Create a grid bound to the container
  83. Grid grid = new Grid(container);
  84. grid.setColumnOrder("name", "born");
  85. layout.addComponent(grid);
  86. ----
  87. Note that you need to override [methodname]#equals()# and [methodname]#hashcode()# for
  88. the bean ([classname]#Person#) class to make the [classname]#BeanItemContainer# work properly.
  89. [[components.grid.basic.manual]]
  90. === Default Data Source and Shorthands
  91. Sometimes, when you have just a few fixed items that you want to display, you
  92. can define the grid columns and add data rows manually. [classname]#Grid# is by
  93. default bound to a [classname]#IndexedContainer#. You can define new columns
  94. (container properties) with [methodname]#addColumn()# and then add rows (items)
  95. with [methodname]#addRow()#. The types in the row data must match the defined
  96. column types.
  97. For example:
  98. [source, java]
  99. ----
  100. // Create a grid
  101. Grid grid = new Grid();
  102. // Define some columns
  103. grid.addColumn("name", String.class);
  104. grid.addColumn("born", Integer.class);
  105. // Add some data rows
  106. grid.addRow("Nicolaus Copernicus", 1543);
  107. grid.addRow("Galileo Galilei", 1564);
  108. grid.addRow("Johannes Kepler", 1571);
  109. layout.addComponent(grid);
  110. ----
  111. Or, if you have the data in an array:
  112. [source, java]
  113. ----
  114. // Have some data
  115. Object[][] people = { {"Nicolaus Copernicus", 1543},
  116. {"Galileo Galilei", 1564},
  117. {"Johannes Kepler", 1571}};
  118. for (Object[] person: people)
  119. grid.addRow(person);
  120. ----
  121. Note that you can not use [methodname]#addRow()# to add items if the container
  122. is read-only or has read-only columns, such as generated columns.
  123. [[components.grid.selection]]
  124. == Handling Selection Changes
  125. Selection in [classname]#Grid# is handled a bit differently from other selection
  126. components, as it is not an [classname]#AbstractSelect#. Grid supports both
  127. single and multiple selection, defined by the __selection mode__. Selection
  128. events can be handled with a [interfacename]#SelectionListener#.
  129. [[components.grid.selection.mode]]
  130. === Selection Mode
  131. A [classname]#Grid# can be set to be in [literal]#++SINGLE++# (default),
  132. [literal]#++MULTI++#, or [literal]#++NONE++# selection mode, defined in the
  133. [classname]#Grid.SelectionMode# enum.
  134. [source, java]
  135. ----
  136. // Use single-selection mode (default)
  137. grid.setSelectionMode(SelectionMode.SINGLE);
  138. ----
  139. Empty (null) selection is allowed by default, but can be disabled
  140. with [methodname]#setDeselectAllowed()# in single-selection mode.
  141. The selection is handled with a different selection model object in each
  142. respective selection mode: [classname]#SingleSelectionModel#,
  143. [classname]#MultiSelectionModel#, and [classname]#NoSelectionModel# (in which
  144. selection is always empty).
  145. [source, java]
  146. ----
  147. // Pre-select an item
  148. SingleSelectionModel selection =
  149. (SingleSelectionModel) grid.getSelectionModel();
  150. selection.select( // Select 3rd item
  151. grid.getContainerDataSource().getIdByIndex(2));
  152. ----
  153. [[components.grid.selection.single]]
  154. === Handling Selection
  155. Changes in the selection can be handled with a
  156. [interfacename]#SelectionListener#. You need to implement the
  157. [methodname]#select()# method, which gets a [classname]#SelectionEvent# as
  158. parameter. In addition to selection, you can handle clicks on rows or cells with
  159. a [interfacename]#ItemClickListener#.
  160. You can get the new selection from the selection event with
  161. [methodname]#getSelected()#, which returns a set of item IDs, or more simply
  162. from the grid or the selection model with [methodname]#getSelectedRow()#, which
  163. returns the single selected item ID.
  164. For example:
  165. [source, java]
  166. ----
  167. grid.addSelectionListener(selectionEvent -> { // Java 8
  168. // Get selection from the selection model
  169. Object selected = ((SingleSelectionModel)
  170. grid.getSelectionModel()).getSelectedRow();
  171. if (selected != null)
  172. Notification.show("Selected " +
  173. grid.getContainerDataSource().getItem(selected)
  174. .getItemProperty("name"));
  175. else
  176. Notification.show("Nothing selected");
  177. });
  178. ----
  179. The current selection can be obtained from the [classname]#Grid# object by
  180. [methodname]#getSelectedRow()# or [methodname]#getSelectedRows()#, which return
  181. one (in single-selection mode) or all (in multi-selection mode) selected items.
  182. [WARNING]
  183. ====
  184. Note that changes to the item set of the container data source are not
  185. automatically reflected in the selection model and may cause the selection model
  186. to refer to stale item IDs. This always occurs, for example, when you delete the
  187. selected item or items. So, if you modify the item set of the container, you
  188. should synchronize or reset the selection with the container, such as by calling
  189. [methodname]#reset()# on the selection model.
  190. ====
  191. [[components.grid.selection.multi]]
  192. === Multiple Selection
  193. In the multiple selection mode, a user can select multiple items by clicking on
  194. the checkboxes in the leftmost column, or by using the kbd:[Space] to select/deselect the currently focused row.
  195. Space bar is the default key for toggling the selection, but it can be customized.
  196. [[figure.components.grid.selection.multi]]
  197. .Multiple Selection in [classname]#Grid#
  198. image::img/grid-selection-multi.png[width=50%, scaledwidth=75%]
  199. The selection is managed through the [classname]#MultiSelectionMode# class. The
  200. currently selected rows can be set with [methodname]#setSelected()# by a
  201. collection of item IDs, or you can use [methodname]#select()# to add items to
  202. the selection.
  203. [source, java]
  204. ----
  205. // Grid in multi-selection mode
  206. Grid grid = new Grid(exampleDataSource());
  207. grid.setSelectionMode(SelectionMode.MULTI);
  208. // Pre-select some items
  209. MultiSelectionModel selection =
  210. (MultiSelectionModel) grid.getSelectionModel();
  211. selection.setSelected( // Items 2-4
  212. grid.getContainerDataSource().getItemIds(2, 3));
  213. ----
  214. The current selection can be read with [methodname]#getSelectedRows()#; either
  215. in the [classname]#MultiSelectionMode# object or in the [classname]#Grid#.
  216. [source, java]
  217. ----
  218. // Allow deleting the selected items
  219. Button delSelected = new Button("Delete Selected", e -> {
  220. // Delete all selected data items
  221. for (Object itemId: selection.getSelectedRows())
  222. grid.getContainerDataSource().removeItem(itemId);
  223. // Otherwise out of sync with container
  224. grid.getSelectionModel().reset();
  225. // Disable after deleting
  226. e.getButton().setEnabled(false);
  227. });
  228. delSelected.setEnabled(grid.getSelectedRows().size() > 0);
  229. ----
  230. Changes in the selection can be handled with a
  231. [interfacename]#SelectionListener#. The selection event object provides
  232. [methodname]#getAdded()# and [methodname]#getRemoved()# to allow determining the
  233. differences in the selection change.
  234. [source, java]
  235. ----
  236. // Handle selection changes
  237. grid.addSelectionListener(selection -> { // Java 8
  238. Notification.show(selection.getAdded().size() +
  239. " items added, " +
  240. selection.getRemoved().size() +
  241. " removed.");
  242. // Allow deleting only if there's any selected
  243. deleteSelected.setEnabled(
  244. grid.getSelectedRows().size() > 0);
  245. });
  246. ----
  247. [[components.grid.selection.disallowuser]]
  248. === Disallowing User Selection
  249. It is possible to prevent the user from changing the selection in grid for both single- and multi-selection models:
  250. [source, java]
  251. ----
  252. HasUserSelectionAllowed model = (HasUserSelectionAllowed) grid.getSelectionModel();
  253. model.setUserSelectionAllowed(false);
  254. ----
  255. [NOTE]
  256. Both `SingleSelectionModel` and `MultiSelectModel` implement `HasUserSelectionAllowed` so the cast is generally safe.
  257. [[components.grid.selection.clicks]]
  258. === Focus and Clicks
  259. In addition to selecting rows, you can focus individual cells. The focus can be
  260. moved with arrow keys and, if editing is enabled, pressing kbd:[Enter] opens the
  261. editor. Normally, pressing kbd:[Tab] or kbd:[Shift+Tab] moves the focus to another component,
  262. as usual.
  263. When editing or in unbuffered mode, kbd:[Tab] or kbd:[Shift+Tab] moves the focus to the next or
  264. previous cell. The focus moves from the last cell of a row forward to the
  265. beginning of the next row, and likewise, from the first cell backward to the
  266. end of the previous row. Note that you can extend [classname]#DefaultEditorEventHandler#
  267. to change this behavior.
  268. With the mouse, you can focus a cell by clicking on it. The clicks can be handled
  269. with an [interfacename]#ItemClickListener#. The [classname]#ItemClickEvent#
  270. object contains various information, most importantly the ID of the clicked row
  271. and column.
  272. [source, java]
  273. ----
  274. grid.addItemClickListener(event -> // Java 8
  275. Notification.show("Value: " +
  276. container.getContainerProperty(event.getItemId(),
  277. event.getPropertyId()).getValue().toString()));
  278. ----
  279. The clicked grid cell is also automatically focused.
  280. The focus indication is themed so that the focused cell has a visible focus
  281. indicator style by default, while the row doesn't. You can enable row focus, as
  282. well as disable cell focus, in a custom theme. See <<components.grid.css>>.
  283. [[components.grid.columns]]
  284. == Configuring Columns
  285. Columns are normally defined in the container data source. The
  286. [methodname]#addColumn()# method can be used to add columns to a container that
  287. supports it, such as the default [classname]#IndexedContainer#.
  288. Column configuration is defined in [classname]#Grid.Column# objects, which can
  289. be obtained from the grid with [methodname]#getColumn()# by the column
  290. (property) ID.
  291. [source, java]
  292. ----
  293. Grid.Column bornColumn = grid.getColumn("born");
  294. bornColumn.setHeaderCaption("Born");
  295. ----
  296. In the following, we describe the basic column configuration.
  297. [[components.grid.columns.order]]
  298. === Column Order
  299. You can set the order of columns with [methodname]#setColumnOrder()# for the
  300. grid. Columns that are not given for the method are placed after the specified
  301. columns in their natural order.
  302. [source, java]
  303. ----
  304. grid.setColumnOrder("firstname", "lastname", "born",
  305. "birthplace", "died");
  306. ----
  307. Note that the method can not be used to hide columns. You can hide columns with
  308. the [methodname]#removeColumn()#, as described later, or by hiding them in a
  309. [classname]#GeneratedPropertyContainer#.
  310. [[components.grid.columns.removing]]
  311. === Hiding Columns
  312. Columns can be hidden by removing them with [methodname]#removeColumn()#. You
  313. can remove all columns with [methodname]#removeAllColumns()#. The removed columns
  314. are only removed from the grid, not from the container data source.
  315. To restore a previously removed column, you can call [methodname]#addColumn()#
  316. with the property ID. Instead of actually adding another column to the data
  317. source, it merely restores the previously removed one. However, column settings
  318. such as header or editor are not restored, but must be redone.
  319. You can also hide columns at container-level. At least
  320. [classname]#GeneratedpropertyContainer# allows doing so, as described in
  321. <<dummy/../../../framework/datamodel/datamodel-container#datamodel.container.gpc,"GeneratedPropertyContainer">>.
  322. [[components.grid.columns.captions]]
  323. === Column Captions
  324. Column captions are displayed in the grid header. The default captions are
  325. generated automatically from the property ID. You can set the header caption
  326. explicitly through the column object with [methodname]#setHeaderCaption()#.
  327. [source, java]
  328. ----
  329. Grid.Column bornColumn = grid.getColumn("born");
  330. bornColumn.setHeaderCaption("Born");
  331. ----
  332. This is equivalent to setting it with [methodname]#setText()# for the header
  333. cell; the [classname]#HeaderCell# also allows setting the caption in HTML or as
  334. a component, as well as styling it, as described later in
  335. <<components.grid.headerfooter>>.
  336. [[components.grid.columns.width]]
  337. === Column Widths
  338. Columns have by default undefined width, which causes automatic sizing based on
  339. the widths of the displayed data. You can set column widths explicitly by pixel
  340. value with [methodname]#setWidth()#, or relatively using expand ratios with
  341. [methodname]#setExpandRatio()#.
  342. When using expand ratios, the columns with a non-zero expand ratio use the extra
  343. space remaining from other columns, in proportion to the defined ratios.
  344. You can specify minimum and maximum widths for the expanding columns with
  345. [methodname]#setMinimumWidth()# and [methodname]#setMaximumWidth()#,
  346. respectively.
  347. The user can resize columns by dragging their separators with the mouse. When resized manually,
  348. all the columns widths are set to explicit pixel values, even if they had
  349. relative values before.
  350. [[components.grid.columns.frozen]]
  351. === Frozen Columns
  352. You can set the number of columns to be frozen with
  353. [methodname]#setFrozenColumnCount()#, so that they are not scrolled off when
  354. scrolling horizontally.
  355. [source, java]
  356. ----
  357. grid.setFrozenColumnCount(2);
  358. ----
  359. Setting the count to [parameter]#0# disables frozen data columns; setting it to
  360. [parameter]#-1# also disables the selection column in multi-selection mode.
  361. [[components.grid.generatedcolumns]]
  362. == Generating Columns
  363. Columns with values computed from other columns or in some other way can be
  364. generated with a container or data model that generates the property values. The
  365. [classname]#GeneratedPropertyContainer# can be used for this purpose. It wraps
  366. around any indexed container to extend its properties with read-only generated
  367. properties. The generated properties can have same IDs as the original ones,
  368. thereby replacing them with formatted or converted values. See
  369. <<dummy/../../../framework/datamodel/datamodel-container#datamodel.container.gpc,"GeneratedPropertyContainer">>
  370. for a detailed description of using it.
  371. Generated columns are read-only, so you can not add grid rows with
  372. [methodname]#addRow()#. In editable mode, editor fields are not generated for
  373. generated columns.
  374. Note that, while [classname]#GeneratedPropertyContainer# implements
  375. [interfacename]#Container.Sortable#, the wrapped container might not, and also
  376. sorting on the generated properties requires special handling. In such cases,
  377. generated properties or the entire container might not actually be sortable.
  378. [[components.grid.renderer]]
  379. == Column Renderers
  380. A __renderer__ is a feature that draws the client-side representation of a data
  381. value. This allows having images, HTML, and buttons in grid cells.
  382. [[figure.components.grid.renderer]]
  383. .Column renderers: image, date, HTML, and button
  384. image::img/grid-renderers.png[width=75%, scaledwidth=100%]
  385. Renderers implement the [interfacename]#Renderer# interface.
  386. You set the column renderer in the [classname]#Grid.Column# object as follows:
  387. [source, java]
  388. ----
  389. grid.addColumn("born", Integer.class);
  390. ...
  391. Grid.Column bornColumn = grid.getColumn("born");
  392. bornColumn.setRenderer(new NumberRenderer("born in %d AD"));
  393. ----
  394. Renderers require a specific data type for the column. To convert to a property
  395. type to a type required by a renderer, you can pass an optional
  396. [interfacename]#Converter# to [methodname]#setRenderer()#, as described later in
  397. this section. A converter can also be used to (pre)format the property values.
  398. The converter is run on the server-side, before sending the values to the
  399. client-side to be rendered with the renderer.
  400. The following renderers are available, as defined in the server-side
  401. [package]#com.vaadin.ui.renderers# package:
  402. [classname]#ButtonRenderer#:: Renders the data value as the caption of a button. A [interfacename]#RendererClickListener# can be given to handle the button clicks.
  403. ifdef::web[]
  404. +
  405. Typically, a button renderer is used to display buttons for operating on a data
  406. item, such as edit, view, delete, etc. It is not meaningful to store the button
  407. captions in the data source, rather you want to generate them, and they are
  408. usually all identical.
  409. +
  410. [source, java]
  411. ----
  412. BeanItemContainer<Person> people =
  413. new BeanItemContainer<>(Person.class);
  414. people.addBean(new Person("Nicolaus Copernicus", 1473));
  415. people.addBean(new Person("Galileo Galilei", 1564));
  416. people.addBean(new Person("Johannes Kepler", 1571));
  417. // Generate button caption column
  418. GeneratedPropertyContainer gpc =
  419. new GeneratedPropertyContainer(people);
  420. gpc.addGeneratedProperty("delete",
  421. new PropertyValueGenerator<String>() {
  422. @Override
  423. public String getValue(Item item, Object itemId,
  424. Object propertyId) {
  425. return "Delete"; // The caption
  426. }
  427. @Override
  428. public Class<String> getType() {
  429. return String.class;
  430. }
  431. });
  432. // Create a grid
  433. Grid grid = new Grid(gpc);
  434. // Render a button that deletes the data row (item)
  435. grid.getColumn("delete")
  436. .setRenderer(new ButtonRenderer(e -> // Java 8
  437. grid.getContainerDataSource()
  438. .removeItem(e.getItemId())));
  439. ----
  440. endif::web[]
  441. [classname]#ImageRenderer#:: Renders the cell as an image.
  442. The column type must be a [interfacename]#Resource#, as described in
  443. <<dummy/../../../framework/application/application-resources#application.resources,"Images and Other Resources">>; only [classname]#ThemeResource# and
  444. [classname]#ExternalResource# are currently supported for images in
  445. [classname]#Grid#.
  446. ifdef::web[]
  447. +
  448. [source, java]
  449. ----
  450. grid.addColumn("picture", Resource.class)
  451. .setRenderer(new ImageRenderer());
  452. ...
  453. // Add some data rows
  454. grid.addRow(new ThemeResource("img/copernicus-128px.jpg"),
  455. "Nicolaus Copernicus", 1543);
  456. grid.addRow(new ThemeResource("img/galileo-128px.jpg"),
  457. "Galileo Galilei", 1564);
  458. ----
  459. +
  460. Instead of creating the resource objects explicitly, as was done above, you
  461. could generate them dynamically from file name strings using a
  462. [interfacename]#Converter# for the column.
  463. +
  464. [source, java]
  465. ----
  466. // Define some columns
  467. grid.addColumn("picture", String.class); // Filename
  468. grid.addColumn("name", String.class);
  469. // Set the image renderer
  470. grid.getColumn("picture").setRenderer(new ImageRenderer(),
  471. new Converter<Resource, String>() {
  472. @Override
  473. public String convertToModel(Resource value,
  474. Class<? extends String> targetType, Locale l)
  475. throws Converter.ConversionException {
  476. return "not needed";
  477. }
  478. @Override
  479. public Resource convertToPresentation(String value,
  480. Class<? extends Resource> targetType, Locale l)
  481. throws Converter.ConversionException {
  482. return new ThemeResource("img/" + value);
  483. }
  484. @Override
  485. public Class<String> getModelType() {
  486. return String.class;
  487. }
  488. @Override
  489. public Class<Resource> getPresentationType() {
  490. return Resource.class;
  491. }
  492. });
  493. // Add some data rows
  494. grid.addRow("copernicus-128px.jpg", "Nicolaus Copernicus");
  495. grid.addRow("galileo-128px.jpg", "Galileo Galilei");
  496. grid.addRow("kepler-128px.jpg", "Johannes Kepler");
  497. ----
  498. +
  499. You also need to define the row heights so that the images fit there. You can
  500. set it in the theme for all data cells or for the column containing the images.
  501. +
  502. For the latter way, first define a CSS style name for grid and the column:
  503. +
  504. [source, java]
  505. ----
  506. grid.setStyleName("gridwithpics128px");
  507. grid.setCellStyleGenerator(cell ->
  508. "picture".equals(cell.getPropertyId())?
  509. "imagecol" : null);
  510. ----
  511. ifdef::web[]
  512. +
  513. Then, define the style in CSS (Sass):
  514. endif::web[]
  515. +
  516. [source, css]
  517. ----
  518. .gridwithpics128px .imagecol {
  519. height: 128px;
  520. background: black;
  521. text-align: center;
  522. }
  523. ----
  524. endif::web[]
  525. [classname]#DateRenderer#:: Formats a column with a [classname]#Date# type using string formatter. The
  526. format string is same as for [methodname]#String.format()# in Java API. The date
  527. is passed in the parameter index 1, which can be omitted if there is only one
  528. format specifier, such as "[literal]#++%tF++#".
  529. ifdef::web[]
  530. +
  531. [source, java]
  532. ----
  533. Grid.Column bornColumn = grid.getColumn("born");
  534. bornColumn.setRenderer(
  535. new DateRenderer("%1$tB %1$te, %1$tY",
  536. Locale.ENGLISH));
  537. ----
  538. +
  539. Optionally, a locale can be given. Otherwise, the default locale (in the
  540. component tree) is used.
  541. endif::web[]
  542. [classname]#HTMLRenderer#:: Renders the cell as HTML. This allows formatting cell content, as well as using
  543. HTML features such as hyperlinks.
  544. ifdef::web[]
  545. +
  546. First, set the renderer in the [classname]#Grid.Column# object:
  547. +
  548. [source, java]
  549. ----
  550. grid.addColumn("link", String.class)
  551. .setRenderer(new HtmlRenderer());
  552. ----
  553. ifdef::web[]
  554. +
  555. Then, in the grid data, give the cell content:
  556. endif::web[]
  557. +
  558. [source, java]
  559. ----
  560. grid.addRow("Nicolaus Copernicus", 1543,
  561. "<a href='http://en.wikipedia.org/wiki/" +
  562. "Nicolaus_Copernicus' target='_top'>info</a>");
  563. ----
  564. +
  565. You could also use a [interfacename]#PropertyFormatter# or a generated column to
  566. generate the HTML for the links.
  567. endif::web[]
  568. [classname]#NumberRenderer#:: Formats column values with a numeric type extending [classname]#Number#:
  569. [classname]#Integer#, [classname]#Double#, etc. The format can be specified
  570. either by the subclasses of [classname]#java.text.NumberFormat#, namely
  571. [classname]#DecimalFormat# and [classname]#ChoiceFormat#, or by
  572. [methodname]#String.format()#.
  573. ifdef::web[]
  574. +
  575. For example:
  576. +
  577. [source, java]
  578. ----
  579. // Define some columns
  580. grid.addColumn("name", String.class);
  581. grid.addColumn("born", Integer.class);
  582. grid.addColumn("sletters", Integer.class);
  583. grid.addColumn("rating", Double.class);
  584. // Use decimal format
  585. grid.getColumn("born").setRenderer(new NumberRenderer(
  586. new DecimalFormat("in #### AD")));
  587. // Use textual formatting on numeric ranges
  588. grid.getColumn("sletters").setRenderer(new NumberRenderer(
  589. new ChoiceFormat("0#none|1#one|2#multiple")));
  590. // Use String.format() formatting
  591. grid.getColumn("rating").setRenderer(new NumberRenderer(
  592. "%02.4f", Locale.ENGLISH));
  593. // Add some data rows
  594. grid.addRow("Nicolaus Copernicus", 1473, 2, 0.4);
  595. grid.addRow("Galileo Galilei", 1564, 0, 4.2);
  596. grid.addRow("Johannes Kepler", 1571, 1, 2.3);
  597. ----
  598. endif::web[]
  599. [classname]#ProgressBarRenderer#:: Renders a progress bar in a column with a [classname]#Double# type. The value
  600. must be between 0.0 and 1.0.
  601. ifdef::web[]
  602. +
  603. For example:
  604. +
  605. [source, java]
  606. ----
  607. // Define some columns
  608. grid.addColumn("name", String.class);
  609. grid.addColumn("rating", Double.class)
  610. .setRenderer(new ProgressBarRenderer());
  611. // Add some data rows
  612. grid.addRow("Nicolaus Copernicus", 0.1);
  613. grid.addRow("Galileo Galilei", 0.42);
  614. grid.addRow("Johannes Kepler", 1.0);
  615. ----
  616. endif::web[]
  617. [classname]#TextRenderer#:: Displays plain text as is. Any HTML markup is quoted.
  618. [[components.grid.renderer.custom]]
  619. === Custom Renderers
  620. Renderers are component extensions that require a client-side counterpart. See
  621. <<dummy/../../../framework/clientsidewidgets/clientsidewidgets-grid#clientsidewidgets.grid.renderers,"Renderers">>
  622. for information on implementing custom renderers.
  623. [[components.grid.renderer.converter]]
  624. === Converting for Rendering
  625. Optionally, you can give a [interfacename]#Converter# in the
  626. [methodname]#setRenderer()#, or define it for the column, to convert the data
  627. value to an intermediary representation that is rendered by the renderer. For
  628. example, when using an [classname]#ImageRenderer#, you could store the image file name
  629. in the data column, which the converter would convert to a resource, which would
  630. then be rendered by the renderer.
  631. In the following example, we use a converter and [classname]#HTMLRenderer# to display boolean
  632. values as [classname]#FontAwesome# icons
  633. [source, java]
  634. ----
  635. // Have a column for hyperlink paths to Wikipedia
  636. grid.addColumn("truth", Boolean.class);
  637. Grid.Column truth = grid.getColumn("truth");
  638. truth.setRenderer(new HtmlRenderer(),
  639. new StringToBooleanConverter(
  640. FontAwesome.CHECK_CIRCLE_O.getHtml(),
  641. FontAwesome.CIRCLE_O.getHtml()));
  642. ...
  643. ----
  644. In this example, we use a converter to format URL paths to complete
  645. HTML hyperlinks with [classname]#HTMLRenderer#:
  646. [source, java]
  647. ----
  648. // Have a column for hyperlink paths to Wikipedia
  649. grid.addColumn("link", String.class);
  650. Grid.Column linkColumn = grid.getColumn("link");
  651. linkColumn.setRenderer(new HtmlRenderer(),
  652. new Converter<String,String>(){
  653. @Override
  654. public String convertToModel(String value,
  655. Class<? extends String> targetType, Locale locale)
  656. throws Converter.ConversionException {
  657. return "not implemented";
  658. }
  659. @Override
  660. public String convertToPresentation(String value,
  661. Class<? extends String> targetType, Locale locale)
  662. throws Converter.ConversionException {
  663. return "<a href='http://en.wikipedia.org/wiki/" +
  664. value + "' target='_blank'>more info</a>";
  665. }
  666. @Override
  667. public Class<String> getModelType() {
  668. return String.class;
  669. }
  670. @Override
  671. public Class<String> getPresentationType() {
  672. return String.class;
  673. }
  674. });
  675. // Data with a hyperlink path in the third column
  676. grid.addRow("Nicolaus Copernicus", 1473,
  677. "Nicolaus_Copernicus");
  678. ...
  679. ----
  680. A [classname]#GeneratedPropertyContainer# could be used for much the same
  681. purpose.
  682. [[components.grid.headerfooter]]
  683. == Header and Footer
  684. A grid by default has a header, which displays column names, and can have a
  685. footer. Both can have multiple rows and neighbouring header row cells can be
  686. joined to feature column groups.
  687. [[components.grid.headerfooter.adding]]
  688. === Adding and Removing Header and Footer Rows
  689. A new header row is added with [methodname]#prependHeaderRow()#, which adds it
  690. at the top of the header, [methodname]#appendHeaderRow()#, which adds it at the
  691. bottom of the header, or with [methodname]#addHeaderRowAt()#, which inserts it
  692. at the specified 0-base index. All of the methods return a
  693. [classname]#HeaderRow# object, which you can use to work on the header further.
  694. [source, java]
  695. ----
  696. // Group headers by joining the cells
  697. HeaderRow groupingHeader = grid.prependHeaderRow();
  698. ...
  699. // Create a header row to hold column filters
  700. HeaderRow filterRow = grid.appendHeaderRow();
  701. ...
  702. ----
  703. Similarly, you can add footer rows with [methodname]#appendFooterRow()#,
  704. [methodname]#prependFooterRow()#, and [methodname]#addFooterRowAt()#.
  705. [[components.grid.headerfooter.joining]]
  706. === Joining Header and Footer Cells
  707. You can join two or more header or footer cells with the [methodname]#join()#
  708. method. For header cells, the intention is usually to create column grouping,
  709. while for footer cells, you typically calculate sums or averages.
  710. [source, java]
  711. ----
  712. // Group headers by joining the cells
  713. HeaderRow groupingHeader = grid.prependHeaderRow();
  714. HeaderCell namesCell = groupingHeader.join(
  715. groupingHeader.getCell("firstname"),
  716. groupingHeader.getCell("lastname")).setText("Person");
  717. HeaderCell yearsCell = groupingHeader.join(
  718. groupingHeader.getCell("born"),
  719. groupingHeader.getCell("died"),
  720. groupingHeader.getCell("lived")).setText("Dates of Life");
  721. ----
  722. [[components.grid.headerfooter.content]]
  723. === Text and HTML Content
  724. You can set the header caption with [methodname]#setText()#, in which case any
  725. HTML formatting characters are quoted to ensure security.
  726. [source, java]
  727. ----
  728. HeaderRow mainHeader = grid.getDefaultHeaderRow();
  729. mainHeader.getCell("firstname").setText("First Name");
  730. mainHeader.getCell("lastname").setText("Last Name");
  731. mainHeader.getCell("born").setText("Born In");
  732. mainHeader.getCell("died").setText("Died In");
  733. mainHeader.getCell("lived").setText("Lived For");
  734. ----
  735. To use raw HTML in the captions, you can use [methodname]#setHtml()#.
  736. [source, java]
  737. ----
  738. namesCell.setHtml("<b>Names</b>");
  739. yearsCell.setHtml("<b>Years</b>");
  740. ----
  741. [[components.grid.headerfooter.components]]
  742. === Components in Header or Footer
  743. You can set a component in a header or footer cell with
  744. [methodname]#setComponent()#. Often, this feature is used to allow filtering, as
  745. described in <<components.grid.filtering>>, which also gives an example of the
  746. use.
  747. [[components.grid.filtering]]
  748. == Filtering
  749. The ability to include components in the grid header can be used to create
  750. filters for the grid data. Filtering is done in the container data source, so
  751. the container must be of type that implements
  752. [interfacename]#Container.Filterable#.
  753. [[figure.components.grid.filtering]]
  754. .Filtering Grid
  755. image::img/grid-filtering.png[width=50%, scaledwidth=80%]
  756. The filtering illustrated in <<figure.components.grid.filtering>> can be created
  757. as follows:
  758. [source, java]
  759. ----
  760. // Have a filterable container
  761. IndexedContainer container = exampleDataSource();
  762. // Create a grid bound to it
  763. Grid grid = new Grid(container);
  764. grid.setSelectionMode(SelectionMode.NONE);
  765. grid.setWidth("500px");
  766. grid.setHeight("300px");
  767. // Create a header row to hold column filters
  768. HeaderRow filterRow = grid.appendHeaderRow();
  769. // Set up a filter for all columns
  770. for (Object pid: grid.getContainerDataSource()
  771. .getContainerPropertyIds()) {
  772. HeaderCell cell = filterRow.getCell(pid);
  773. // Have an input field to use for filter
  774. TextField filterField = new TextField();
  775. filterField.setColumns(8);
  776. // Update filter When the filter input is changed
  777. filterField.addTextChangeListener(change -> {
  778. // Can't modify filters so need to replace
  779. container.removeContainerFilters(pid);
  780. // (Re)create the filter if necessary
  781. if (! change.getText().isEmpty())
  782. container.addContainerFilter(
  783. new SimpleStringFilter(pid,
  784. change.getText(), true, false));
  785. });
  786. cell.setComponent(filterField);
  787. }
  788. ----
  789. [[components.grid.sorting]]
  790. == Sorting
  791. A user can sort the data in a grid on a column by clicking the column header.
  792. Clicking another time on the current sort column reverses the sort direction.
  793. Clicking on other column headers while keeping the Shift key pressed adds a
  794. secondary or more sort criteria.
  795. [[figure.components.grid.sorting]]
  796. .Sorting Grid on Multiple Columns
  797. image::img/grid-sorting.png[width=50%, scaledwidth=75%]
  798. Defining sort criteria programmatically can be done with the various
  799. alternatives of the [methodname]#sort()# method. You can sort on a specific
  800. column with [methodname]#sort(Object propertyId)#, which defaults to ascending
  801. sorting order, or [methodname]#sort(Object propertyId, SortDirection
  802. direction)#, which allows specifying the sort direction.
  803. [source, java]
  804. ----
  805. grid.sort("name", SortDirection.DESCENDING);
  806. ----
  807. To sort on multiple columns, you need to use the fluid sort API with
  808. [methodname]#sort(Sort)#, which allows chaining sorting rules. Sorting rules are
  809. created with the static [methodname]#by()# method, which defines the primary
  810. sort column, and [methodname]#then()#, which can be used to specify any
  811. secondary sort columns. They default to ascending sort order, but the sort
  812. direction can be given with an optional parameter.
  813. [source, java]
  814. ----
  815. // Sort first by city and then by name
  816. grid.sort(Sort.by("city", SortDirection.ASCENDING)
  817. .then("name", SortDirection.DESCENDING));
  818. ----
  819. The container data source must support sorting. At least, it must implement
  820. [interfacename]#Container.Sortable#. Note that when using
  821. [classname]#GeneratedPropertyContainer#, as described in
  822. <<components.grid.generatedcolumns>>, even though the container implements the
  823. interface, the wrapped container must also support it. Also, the generated
  824. properties are not normally sortable, but require special handling to enable
  825. sorting.
  826. [[components.grid.editing]]
  827. == Editing
  828. Grid supports line-based editing, where double-clicking a row opens the row
  829. editor. In the editor, the input fields can be edited, as well as navigated with
  830. kbd:[Tab] and kbd:[Shift+Tab] keys. If validation fails, an error is displayed and the user
  831. can correct the inputs.
  832. To enable editing, you need to call [methodname]#setEditorEnabled(true)# for the
  833. grid.
  834. [source, java]
  835. ----
  836. Grid grid = new Grid(GridExample.exampleDataSource());
  837. grid.setEditorEnabled(true);
  838. ----
  839. Grid supports two row editor modes - buffered and unbuffered. The default mode is
  840. buffered. The mode can be changed with [methodname]#setBuffered(false)#
  841. [[components.grid.editing.buffered]]
  842. === Buffered Mode
  843. The editor has a [guibutton]#Save# button that commits
  844. the data item to the container data source and closes the editor. The
  845. [guibutton]#Cancel# button discards the changes and exits the editor.
  846. A row under editing is illustrated in <<figure.components.grid.editing>>.
  847. [[figure.components.grid.editing]]
  848. .Editing a Grid Row
  849. image::img/grid-editor-basic.png[width=50%, scaledwidth=75%]
  850. [[components.grid.editing.unbuffered]]
  851. === Unbuffered Mode
  852. The editor has no buttons and all changed data is committed directly
  853. to the container. If another row is clicked, the editor for the current row is closed and
  854. a row editor for the clicked row is opened.
  855. [[components.grid.editing.fields]]
  856. === Editor Fields
  857. The editor fields are by default generated with a [interfacename]#FieldFactory#
  858. and bound to the container data source with a [classname]#FieldGroup#, which
  859. also handles tasks such as validation, as explained later.
  860. To disable editing in a particular column, you can call
  861. [methodname]#setEditable()# in the [classname]#Column# object with
  862. [parameter]#false# parameter.
  863. [[components.grid.editing.editorfields]]
  864. === Customizing Editor Fields
  865. The editor fields are normally created by the field factory of the editor's field
  866. group, which creates the fields according to the data types of their respective
  867. columns. To customize the editor fields of specific properties, such as to style
  868. them or to set up validation, you can provide them with
  869. [methodname]#setEditorField()# in the respective columns.
  870. In the following example, we configure a field with validation and styling:
  871. [source, java]
  872. ----
  873. TextField nameEditor = new TextField();
  874. // Custom CSS style
  875. nameEditor.addStyleName("nameeditor");
  876. // Custom validation
  877. nameEditor.addValidator(new RegexpValidator(
  878. "^\\p{Alpha}+ \\p{Alpha}+$",
  879. "Need first and last name"));
  880. grid.getColumn("name").setEditorField(nameEditor);
  881. ----
  882. Setting an editor field to [parameter]#null# deletes the currently existing
  883. editor field, whether it was automatically generated or set explicitly with the
  884. setter. It will be regenerated with the factory the next time it is needed.
  885. ifdef::web[]
  886. [[components.grid.editing.captions]]
  887. === Customizing Editor Buttons
  888. In the buffered mode, the editor has two buttons: [guibutton]#Save# and [guibutton]#Cancel#. You can
  889. set their captions with [methodname]#setEditorSaveCaption()# and
  890. [methodname]#setEditorCancelCaption()#, respectively.
  891. In the following example, we demonstrate one way to translate the captions:
  892. [source, java]
  893. ----
  894. // Captions are stored in a resource bundle
  895. ResourceBundle bundle = ResourceBundle.getBundle(
  896. MyAppCaptions.class.getName(),
  897. Locale.forLanguageTag("fi")); // Finnish
  898. // Localize the editor button captions
  899. grid.setEditorSaveCaption(
  900. bundle.getString(MyAppCaptions.SaveKey));
  901. grid.setEditorCancelCaption(
  902. bundle.getString(MyAppCaptions.CancelKey));
  903. ----
  904. endif::web[]
  905. [[components.grid.editing.fieldgroup]]
  906. === Binding to Data with a Field Group
  907. Data binding to the item under editing is handled with a
  908. [classname]#FieldGroup#, which you need to set with
  909. [methodname]#setEditorFieldGroup#. This is mostly useful when using
  910. special-purpose field groups, such as [classname]#BeanFieldGroup# to enable bean
  911. validation.
  912. For example, assuming that we want to enable bean validation for a bean such as
  913. the following:
  914. [source, java]
  915. ----
  916. public class Person implements Serializable {
  917. @NotNull
  918. @Size(min=2, max=10)
  919. private String name;
  920. @Min(1)
  921. @Max(130)
  922. private int age;
  923. ...]
  924. ----
  925. We can now use a [classname]#BeanFieldGroup# in the [classname]#Grid# as
  926. follows:
  927. [source, java]
  928. ----
  929. Grid grid = new Grid(exampleBeanDataSource());
  930. grid.setColumnOrder("name", "age");
  931. grid.setEditorEnabled(true);
  932. // Enable bean validation for the data
  933. grid.setEditorFieldGroup(
  934. new BeanFieldGroup<Person>(Person.class));
  935. // Have some extra validation in a field
  936. TextField nameEditor = new TextField();
  937. nameEditor.addValidator(new RegexpValidator(
  938. "^\\p{Alpha}+ \\p{Alpha}+$",
  939. "Need first and last name"));
  940. grid.setEditorField("name", nameEditor);
  941. ----
  942. To use bean validation as in the example above, you need to include an
  943. implementation of the Bean Validation API in the classpath, as described in
  944. <<dummy/../../../framework/datamodel/datamodel-itembinding#datamodel.itembinding.beanvalidation,"Bean
  945. Validation">>.
  946. ifdef::web[]
  947. [[components.grid.editing.validation]]
  948. === Handling Validation Errors
  949. The input fields are validated when the value is updated. The default
  950. error handler displays error indicators in the invalid fields, as well as the
  951. first error in the editor.
  952. [[figure.components.grid.errors]]
  953. .Editing a Grid Row
  954. image::img/grid-editor-errors.png[width=50%, scaledwidth=75%]
  955. You can modify the error handling by implementing a custom
  956. [interfacename]#EditorErrorHandler# or by extending the
  957. [classname]#DefaultEditorErrorHandler#.
  958. endif::web[]
  959. [[components.grid.editing.fieldfactory]]
  960. === Editor Field Factory
  961. The fields are generated by the [classname]#FieldFactory# of the field group;
  962. you can also set it with [methodname]#setEditorFieldFactory()#. Alternatively,
  963. you can create the editor fields explicitly with [methodname]#setEditorField()#.
  964. [[components.grid.scrolling]]
  965. == Programmatic Scrolling
  966. You can scroll to first item with [methodname]#scrollToStart()#, to end with
  967. [methodname]#scrollToEnd()#, or to a specific row with [methodname]#scrollTo()#.
  968. [[components.grid.stylegeneration]]
  969. == Generating Row or Cell Styles
  970. You can style entire rows with a [interfacename]#RowStyleGenerator# or
  971. individual cells with a [interfacename]#CellStyleGenerator#.
  972. [[components.grid.stylegeneration.row]]
  973. === Generating Row Styles
  974. You set a [interfacename]#RowStyleGenerator# to a grid with
  975. [methodname]#setRowStyleGenerator()#. The [methodname]#getStyle()# method gets a
  976. [classname]#RowReference#, which contains various information about the row and
  977. a reference to the grid, and should return a style name or [parameter]#null# if
  978. no style is generated.
  979. For example, to add a style names to rows having certain values in one column,
  980. you can style them as follows:
  981. [source, java]
  982. ----
  983. grid.setRowStyleGenerator(rowRef -> {// Java 8
  984. if (! ((Boolean) rowRef.getItem()
  985. .getItemProperty("alive")
  986. .getValue()).booleanValue())
  987. return "grayed";
  988. else
  989. return null;
  990. });
  991. ----
  992. You could then style the rows with CSS as follows:
  993. [source, css]
  994. ----
  995. .v-grid-row.grayed {
  996. color: gray;
  997. }
  998. ----
  999. [[components.grid.stylegeneration.cell]]
  1000. === Generating Cell Styles
  1001. You set a [interfacename]#CellStyleGenerator# to a grid with
  1002. [methodname]#setCellStyleGenerator()#. The [methodname]#getStyle()# method gets
  1003. a [classname]#CellReference#, which contains various information about the cell
  1004. and a reference to the grid, and should return a style name or [parameter]#null#
  1005. if no style is generated.
  1006. For example, to add a style name to a specific column, you can match on the
  1007. property ID of the column as follows:
  1008. [source, java]
  1009. ----
  1010. grid.setCellStyleGenerator(cellRef -> // Java 8
  1011. "born".equals(cellRef.getPropertyId())?
  1012. "rightalign" : null);
  1013. ----
  1014. You could then style the cells with a CSS rule as follows:
  1015. [source, css]
  1016. ----
  1017. .v-grid-cell.rightalign {
  1018. text-align: right;
  1019. }
  1020. ----
  1021. [[components.grid.css]]
  1022. == Styling with CSS
  1023. [source, css]
  1024. ----
  1025. .v-grid {
  1026. .v-grid-scroller, .v-grid-scroller-horizontal { }
  1027. .v-grid-tablewrapper {
  1028. .v-grid-header {
  1029. .v-grid-row {
  1030. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  1031. }
  1032. }
  1033. .v-grid-body {
  1034. .v-grid-row,
  1035. .v-grid-row-stripe,
  1036. .v-grid-row-has-data {
  1037. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  1038. }
  1039. }
  1040. .v-grid-footer {
  1041. .v-grid-row {
  1042. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  1043. }
  1044. }
  1045. }
  1046. .v-grid-header-deco { }
  1047. .v-grid-footer-deco { }
  1048. .v-grid-horizontal-scrollbar-deco { }
  1049. .v-grid-editor {
  1050. .v-grid-editor-cells { }
  1051. .v-grid-editor-footer {
  1052. .v-grid-editor-message { }
  1053. .v-grid-editor-buttons {
  1054. .v-grid-editor-save { }
  1055. .v-grid-editor-cancel { }
  1056. }
  1057. }
  1058. }
  1059. }
  1060. ----
  1061. A [classname]#Grid# has an overall [literal]#++v-grid++# style. The actual grid
  1062. has three parts: a header, a body, and a footer. The scrollbar is a custom
  1063. element with [literal]#++v-grid-scroller++# style. In addition, there are some
  1064. decoration elements.
  1065. Grid cells, whether thay are in the header, body, or footer, have a basic
  1066. [literal]#++v-grid-cell++# style. Cells in a frozen column additionally have a
  1067. [literal]#++frozen++# style. Rows have [literal]#++v-grid-row++# style, and
  1068. every other row has additionally a [literal]#++v-grid-row-stripe++# style.
  1069. The focused row has additionally [literal]#++v-grid-row-focused++# style and
  1070. focused cell [literal]#++v-grid-cell-focused++#. By default, cell focus is
  1071. visible, with the border stylable with [parameter]#$v-grid-cell-focused-border#
  1072. parameter in Sass. Row focus has no visible styling, but can be made visible
  1073. with the [parameter]#$v-grid-row-focused-background-color# parameter or with a
  1074. custom style rule.
  1075. In editing mode, a [literal]#++v-grid-editor++# overlay is placed on the row
  1076. under editing. In addition to the editor field cells, it has an error message
  1077. element, as well as the buttons.
  1078. ((()))