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 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. ---
  2. title: Grid
  3. order: 24
  4. layout: page
  5. ---
  6. [[components.grid]]
  7. = 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. [[components.grid.overview]]
  13. == Overview
  14. [classname]#Grid# is for displaying and editing tabular data laid out in rows
  15. and columns. At the top, a __header__ can be shown, and a __footer__ at the
  16. bottom. In addition to plain text, the header and footer can contain HTML and
  17. components. Having components in the header allows implementing filtering
  18. easily. The grid data can be sorted by clicking on a column header;
  19. shift-clicking a column header enables secondary sorting criteria.
  20. [[figure.components.grid.features]]
  21. .A [classname]#Grid#
  22. image::img/grid-features.png[width=70%, scaledwidth=100%]
  23. The data area can be scrolled both vertically and horizontally. The leftmost
  24. columns can be frozen, so that they are never scrolled out of the view. The data
  25. is loaded lazily from the server, so that only the visible data is loaded. The
  26. smart lazy loading functionality gives excellent user experience even with low
  27. bandwidth, such as mobile devices.
  28. TIP: Watch the https://vaadin.com/training/course/view/data-providers-and-grid[Vaadin 8: Data Providers and Grid] free training video to learn more about the DataProvider API, Grid, sorting and filtering.
  29. The grid data can be edited with a row-based editor after double-clicking a row.
  30. The fields are set explicitly, and bound to data.
  31. Grid is fully themeable with CSS and style names can be set for all grid
  32. elements. For data rows and cells, the styles can be generated with a row or
  33. cell style generator.
  34. [[components.grid.data]]
  35. == Binding to Data
  36. [classname]#Grid# is normally used by binding it to a data provider,
  37. described in
  38. <<../datamodel/datamodel-providers.asciidoc#datamodel.dataproviders,"Showing Many Items in a Listing">>.
  39. By default, it is bound to List of items. You can set the items with the
  40. [methodname]#setItems()# method.
  41. For example, if you have a list of beans, you show them in a [classname]#Grid# as follows
  42. [source, java]
  43. ----
  44. // Have some data
  45. List<Person> people = Arrays.asList(
  46. new Person("Nicolaus Copernicus", 1543),
  47. new Person("Galileo Galilei", 1564),
  48. new Person("Johannes Kepler", 1571));
  49. // Create a grid bound to the list
  50. Grid<Person> grid = new Grid<>();
  51. grid.setItems(people);
  52. grid.addColumn(Person::getName).setCaption("Name");
  53. grid.addColumn(Person::getBirthYear).setCaption("Year of birth");
  54. layout.addComponent(grid);
  55. ----
  56. [[components.grid.selection]]
  57. == Handling Selection Changes
  58. Selection in [classname]#Grid# is handled a bit differently from other selection
  59. components, as it is not a [classname]#HasValue#. Grid supports
  60. single, multiple, or no-selection, each defined by a specific selection model. Each
  61. selection model has a specific API depending on the type of the selection.
  62. For basic usage, switching between the built-in selection models is possible by using the
  63. [method]#setSelectionMode(SelectionMode)#. Possible options are [literal]#++SINGLE++# (default),
  64. [literal]#++MULTI++#, or [literal]#++NONE++#.
  65. Listening to selection changes in any selection model is possible with a [classname]#SelectionListener#,
  66. which provides a generic [classname]#SelectionEvent# which for getting the selected value or values.
  67. Note that the listener is actually attached to the selection model and not the grid,
  68. and will stop getting any events if the selection mode is changed.
  69. [source, java]
  70. ----
  71. Grid<Person> grid = new Grid<>();
  72. // switch to multiselect mode
  73. grid.setSelectionMode(SelectionMode.MULTI);
  74. grid.addSelectionListener(event -> {
  75. Set<Person> selected = event.getAllSelectedItems();
  76. Notification.show(selected.size() + " items selected");
  77. });
  78. ----
  79. Programmatically selecting the value is possible via [methodname]#select(T)#.
  80. In multiselect mode, this will add the given item to the selection.
  81. [source, java]
  82. ----
  83. // in single-select, only one item is selected
  84. grid.select(defaultPerson);
  85. // switch to multi select, clears selection
  86. grid.setSelectionMode(SelectionMode.MULTI);
  87. // Select items 2-4
  88. people.subList(2,3).forEach(grid::select);
  89. ----
  90. The current selection can be obtained from the [classname]#Grid# by
  91. [methodname]#getSelectedItems()#, and the returned [classname]#Set# contains either
  92. only one item (in single-selection mode) or several items (in multi-selection mode).
  93. [WARNING]
  94. ====
  95. If you change selection mode for a grid, it will clear the selection
  96. and fire a selection event. To keep the previous selection you must
  97. reset the selection afterwards using the [methodname]#select()# method.
  98. ====
  99. [WARNING]
  100. ====
  101. If you change the grid's items with [methodname]#setItems()# or the used
  102. [classname]#DataProvider#, it will clear the selection and fire a selection event.
  103. To keep the previous selection you must reset the selection afterwards
  104. using the [methodname]#select()# method.
  105. ====
  106. [[components.grid.selection.mode]]
  107. === Selection Models
  108. For more control over the selection, you can access the used selection model with
  109. [methodname]#getSelectionModel()#. The return type is [classname]#GridSelectionModel#
  110. which has generic selection model API, but you can cast that to the specific selection model type,
  111. typically either [classname]#SingleSelectionModel# or [classname]#MultiSelectionModel#.
  112. The selection model is also returned by the [methodname]#setSelectionMode(SelectionMode)# method.
  113. [source, java]
  114. ----
  115. // the default selection model
  116. SingleSelectionModel<Person> defaultModel =
  117. (SingleSelectionModel<Person>) grid.getSelectionModel();
  118. // Use multi-selection mode
  119. MultiSelectionModel<Person> selectionModel =
  120. (MultiSelectionModel<Person>) grid.setSelectionMode(SelectionMode.MULTI);
  121. ----
  122. ==== Single Selection Model
  123. By obtaining a reference to the [classname]#SingleSelectionModel#,
  124. you can access more fine grained API for the single-select case.
  125. The [methodname]#addSingleSelect(SingleSelectionListener)# method provides access to
  126. [classname]#SingleSelectionEvent#, which has some extra API for more convenience.
  127. In single-select mode, it is possible to control whether the empty (null) selection is allowed.
  128. By default it is enabled, but can be disabled with [methodname]#setDeselectAllowed()#.
  129. [source, java]
  130. ----
  131. // preselect value
  132. grid.select(defaultItem);
  133. SingleSelectionModel<Person> singleSelect =
  134. (SingleSelectionModel<Person>) grid.getSelectionModel();
  135. // disallow empty selection
  136. singleSelect.setDeselectAllowed(false);
  137. ----
  138. [[components.grid.selection.multi]]
  139. === Multi-Selection Model
  140. In the multi-selection mode, a user can select multiple items by clicking on
  141. the checkboxes in the leftmost column, or by using the kbd:[Space] to select/deselect the currently focused row.
  142. Space bar is the default key for toggling the selection, but it can be customized.
  143. [[figure.components.grid.selection.multi]]
  144. .Multiple Selection in [classname]#Grid#
  145. image::img/grid-selection-multi.png[width=50%, scaledwidth=75%]
  146. By obtaining a reference to the [classname]#MultiSelectionModel#,
  147. you can access more fine grained API for the multi-select case.
  148. The [classname]#MultiSelectionModel# provides [methodname]#addMultiSelectionListener(MultiSelectionListener)#
  149. access to [classname]#MultiSelectionEvent#, which allows to easily access differences in the selection change.
  150. [source, java]
  151. ----
  152. // Grid in multi-selection mode
  153. Grid<Person> grid = new Grid<>()
  154. grid.setItems(people);
  155. MultiSelectionModel<Person> selectionModel
  156. = (MultiSelectionModel<Person>) grid.setSelectionMode(SelectionMode.MULTI);
  157. selectionModel.selectAll();
  158. selectionModel.addMultiSelectionListener(event -> {
  159. Notification.show(selection.getAddedSelection().size()
  160. + " items added, "
  161. + selection.getRemovedSelection().size()
  162. + " removed.");
  163. // Allow deleting only if there's any selected
  164. deleteSelected.setEnabled(
  165. event.getNewSelection().size() > 0);
  166. });
  167. ----
  168. [[components.grid.selection.clicks]]
  169. === Focus and Clicks
  170. In addition to selecting rows, you can focus individual cells. The focus can be
  171. moved with arrow keys and, if editing is enabled, pressing kbd:[Enter] opens the
  172. editor. Normally, pressing kbd:[Tab] or kbd:[Shift+Tab] moves the focus to another component,
  173. as usual.
  174. When editing or in unbuffered mode, kbd:[Tab] or kbd:[Shift+Tab] moves the focus to the next or
  175. previous cell. The focus moves from the last cell of a row forward to the
  176. beginning of the next row, and likewise, from the first cell backward to the
  177. end of the previous row. Note that you can extend [classname]#DefaultEditorEventHandler#
  178. to change this behavior.
  179. With the mouse, you can focus a cell by clicking on it. The clicks can be handled
  180. with an [interfacename]#ItemClickListener#. The [classname]#ItemClickEvent#
  181. object contains various information, most importantly the ID of the clicked row
  182. and column.
  183. [source, java]
  184. ----
  185. grid.addItemClickListener(event ->
  186. Notification.show("Value: " + event.getItem()));
  187. ----
  188. The clicked grid cell is also automatically focused.
  189. The focus indication is themed so that the focused cell has a visible focus
  190. indicator style by default, while the row does not. You can enable row focus, as
  191. well as disable cell focus, in a custom theme. See <<components.grid.css>>.
  192. [[components.grid.right.clicks]]
  193. === Right-clicks
  194. Right-clicks are supported similar way via `addContextClickListener()` method
  195. [source, java]
  196. ----
  197. grid.addContextClickListener(event -> Notification.show(
  198. ((GridContextClickEvent<Person>)event).getItem() + " Clicked")
  199. );
  200. ----
  201. [[components.grid.columns]]
  202. == Configuring Columns
  203. The [methodname]#addColumn()# method can be used to add columns to [classname]#Grid#.
  204. Column configuration is defined in [classname]#Grid.Column# objects, which are returned by `addColumn` and can also be obtained from the grid with [methodname]#getColumns()#.
  205. The setter methods in [classname]#Column# have _fluent API_, so you can easily chain the configuration calls for columns if you want to.
  206. [source, java]
  207. ----
  208. grid.addColumn(Person::getBirthDate, new DateRenderer())
  209. .setCaption("Birth Date")
  210. .setWidth("100px")
  211. .setResizable(false);
  212. ----
  213. In the following, we describe the basic column configuration.
  214. [[components.grid.columns.automatic]]
  215. === Automatically Adding Columns
  216. You can configure `Grid` to automatically add columns based on the properties in a bean.
  217. To do this, you need to pass the `Class` of the bean type to the constructor when creating a grid.
  218. You can then further configure the columns based on the bean property name.
  219. [source, java]
  220. ----
  221. Grid<Person> grid = new Grid<>(Person.class);
  222. grid.getColumn("birthDate").setWidth("100px");
  223. grid.setItems(people);
  224. ----
  225. [[components.grid.columns.order]]
  226. === Column Order
  227. You can set the order of columns with [methodname]#setColumnOrder()# for the
  228. grid. Columns that are not given for the method are placed after the specified
  229. columns in their natural order.
  230. [source, java]
  231. ----
  232. grid.setColumnOrder(firstnameColumn, lastnameColumn,
  233. bornColumn, birthplaceColumn,
  234. diedColumn);
  235. ----
  236. Note that the method can not be used to hide columns. You can hide columns with
  237. the [methodname]#removeColumn()#, as described later.
  238. [[components.grid.columns.removing]]
  239. === Hiding and Removing Columns
  240. Columns can be hidden by calling [methodname]#setHidden()# in [classname]#Column#.
  241. Furthermore, you can set the columns user hidable using method
  242. [methodname]#setHidable()#.
  243. Columns can be removed with [methodname]#removeColumn()# and
  244. [methodname]#removeAllColumns()#. To restore a previously removed column,
  245. you can call [methodname]#addColumn()#.
  246. [[components.grid.columns.captions]]
  247. === Column Captions
  248. Column captions are displayed in the grid header. You can set the header caption
  249. explicitly through the column object with [methodname]#setCaption()#.
  250. [source, java]
  251. ----
  252. Column<Date> bornColumn = grid.addColumn(Person::getBirthDate);
  253. bornColumn.setCaption("Born date");
  254. ----
  255. This is equivalent to setting it with [methodname]#setText()# for the header
  256. cell; the [classname]#HeaderCell# also allows setting the caption in HTML or as
  257. a component, as well as styling it, as described later in
  258. <<components.grid.headerfooter>>.
  259. [[components.grid.columns.width]]
  260. === Column Widths
  261. Columns have by default undefined width, which causes automatic sizing based on
  262. the widths of the displayed data. You can set column widths explicitly by pixel
  263. value with [methodname]#setWidth()#, or relatively using expand ratios with
  264. [methodname]#setExpandRatio()#.
  265. When using expand ratios, the columns with a non-zero expand ratio use the extra
  266. space remaining from other columns, in proportion to the defined ratios. Do note
  267. that the minimum width of an expanded column by default is based on the contents
  268. of the column (the initially rendered rows). To allow the column to become
  269. narrower than this, use [methodname]#setMinimumWidthFromContent(false)#
  270. (introduced in 8.1).
  271. You can specify minimum and maximum widths for the expanding columns with
  272. [methodname]#setMinimumWidth()# and [methodname]#setMaximumWidth()#,
  273. respectively.
  274. The user can resize columns by dragging their separators with the mouse. When resized manually,
  275. all the columns widths are set to explicit pixel values, even if they had
  276. relative values before.
  277. [[components.grid.columns.frozen]]
  278. === Frozen Columns
  279. You can set the number of columns to be frozen with
  280. [methodname]#setFrozenColumnCount()#, so that they are not scrolled off when
  281. scrolling horizontally.
  282. [source, java]
  283. ----
  284. grid.setFrozenColumnCount(2);
  285. ----
  286. Setting the count to [parameter]#0# disables frozen data columns; setting it to
  287. [parameter]#-1# also disables the selection column in multi-selection mode.
  288. [[components.grid.generatedcolumns]]
  289. == Generating Columns
  290. Columns with values computed from other columns can be simply added by using
  291. lambdas:
  292. [source, java]
  293. ----
  294. // Add generated full name column
  295. Column<String> fullNameColumn = grid.addColumn(person ->
  296. person.getFirstName() + " " + person.getLastName());
  297. fullNameColumn.setCaption("Full name");
  298. ----
  299. [[components.grid.renderer]]
  300. == Column Renderers
  301. A __renderer__ is a feature that draws the client-side representation of a data
  302. value. This allows having images, HTML, and buttons in grid cells.
  303. [[figure.components.grid.renderer]]
  304. .Column renderers: image, date, HTML, and button
  305. image::img/grid-renderers.png[width=75%, scaledwidth=100%]
  306. Renderers implement the [interfacename]#Renderer# interface.
  307. Renderers require a specific data type for the column.
  308. You set the column renderer in the [classname]#Grid.Column# object as follows:
  309. [source, java]
  310. ----
  311. // the type of birthYear is a number
  312. Column<Person, Integer> bornColumn = grid.addColumn(Person::getBirthYear,
  313. new NumberRenderer("born in %d AD"));
  314. ----
  315. Changing the renderer during runtime is also possible, but for type safety
  316. you should store the column reference with data types for doing this.
  317. When you change the renderer, the content of Grid is refreshed.
  318. [source, java]
  319. ----
  320. Column<Person, Integer> ageColumn = grid.addColumn(Person::getBirthYear);
  321. // The default renderer is TextRenderer
  322. addComponent(new Button("Change renderer",
  323. clickEvent -> ageColumn.setRenderer(new NumberRenderer())
  324. ));
  325. ----
  326. The following renderers are available, as defined in the server-side
  327. [package]#com.vaadin.ui.renderers# package:
  328. [classname]#TextRenderer#:: The default renderer, displays plain text as is. Any HTML markup is quoted.
  329. [classname]#ButtonRenderer#:: Renders the data value as the caption of a button. A [interfacename]#RendererClickListener# can be given to handle the button clicks.
  330. +
  331. Typically, a button renderer is used to display buttons for operating on a data
  332. item, such as edit, view, delete, etc. It is not meaningful to store the button
  333. captions in the data source, rather you want to generate them, and they are
  334. usually all identical.
  335. +
  336. [source, java]
  337. ----
  338. List<Person> people = new ArrayList<>();
  339. people.add(new Person("Nicolaus Copernicus", 1473));
  340. people.add(new Person("Galileo Galilei", 1564));
  341. people.add(new Person("Johannes Kepler", 1571));
  342. // Create a grid
  343. Grid<Person> grid = new Grid<>(people);
  344. // Render a button that deletes the data row (item)
  345. grid.addColumn(person -> "Delete",
  346. new ButtonRenderer(clickEvent -> {
  347. people.remove(clickEvent.getItem());
  348. grid.setItems(people);
  349. }));
  350. ----
  351. [classname]#ImageRenderer#:: Renders the cell as an image.
  352. The column type must be a [interfacename]#Resource#, as described in
  353. <<../application/application-resources#application.resources,"Images and Other Resources">>; only [classname]#ThemeResource# and
  354. [classname]#ExternalResource# are currently supported for images in
  355. [classname]#Grid#.
  356. +
  357. [source, java]
  358. ----
  359. Column<Person, ThemeResource> imageColumn = grid.addColumn(
  360. p -> new ThemeResource("img/"+p.getLastname()+".jpg"),
  361. new ImageRenderer());
  362. ----
  363. [classname]#DateRenderer#:: Formats a column with a [classname]#Date# type using string formatter. The
  364. format string is same as for [methodname]#String.format()# in Java API. The date
  365. is passed in the parameter index 1, which can be omitted if there is only one
  366. format specifier, such as "[literal]#++%tF++#".
  367. +
  368. [source, java]
  369. ----
  370. Column<Person, Date> bornColumn = grid.addColumn(Person::getBirthDate,
  371. new DateRenderer("%1$tB %1$te, %1$tY",
  372. Locale.ENGLISH));
  373. ----
  374. +
  375. Optionally, a locale can be given. Otherwise, the default locale (in the
  376. component tree) is used.
  377. [classname]#HTMLRenderer#:: Renders the cell as HTML.
  378. This allows formatting the cell content, as well as using HTML features such as hyperlinks.
  379. +
  380. Set the renderer in the [classname]#Grid.Column# object:
  381. +
  382. [source, java]
  383. ----
  384. Column<Person, String> htmlColumn = grid.addColumn(person ->
  385. "<a href='" + person.getDetailsUrl() + "' target='_top'>info</a>",
  386. new HtmlRenderer());
  387. ----
  388. [classname]#NumberRenderer#:: Formats column values with a numeric type extending [classname]#Number#:
  389. [classname]#Integer#, [classname]#Double#, etc. The format can be specified
  390. either by the subclasses of [classname]#java.text.NumberFormat#, namely
  391. [classname]#DecimalFormat# and [classname]#ChoiceFormat#, or by
  392. [methodname]#String.format()#.
  393. +
  394. For example:
  395. +
  396. [source, java]
  397. ----
  398. // Use decimal format
  399. Column<Integer> birthYear = grid.addColumn(Person::getBirthYear,
  400. new NumberRenderer(new DecimalFormat("in #### AD")));
  401. ----
  402. [classname]#ProgressBarRenderer#:: Renders a progress bar in a column with a [classname]#Double# type. The value
  403. must be between 0.0 and 1.0.
  404. [classname]#LocalDateRenderer#::
  405. Formats a column with the [classname]#LocalDate# type.
  406. The renderer can be constructed with a [classname]#DateTimeFormatter#, or with a custom pattern string.
  407. The locale is either given explicitly with the pattern, resolved from the given [classname]#DateTimeFormatter# or from the grid the renderer is attached to, if neither of the previous are given.
  408. For the pattern string syntax, refer to the following documentation: link:https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns[docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns].
  409. Note we should use `SerializableProvider&lt;DateTimeFormatter&gt;` or lambda in the first case, because
  410. `DateTimeFormatter` is not serializable, and that may lead to problems in certain cases, for instance in a cluster environment.
  411. +
  412. [source, java]
  413. ----
  414. LocalDateRenderer renderer = new LocalDateRenderer(() -> DateTimeFormatter
  415. .ofLocalizedDate(FormatStyle.LONG)
  416. .withLocale(Locale.ENGLISH));
  417. Column<Person, LocalDate> bornColumn =
  418. grid.addColumn(
  419. Person::getBirthDate,
  420. renderer);
  421. // Alternatively, with a custom pattern:
  422. Column<Person, LocalDate> bornColumn =
  423. grid.addColumn(
  424. Person::getBirthDate,
  425. new LocalDateRenderer("yyyy MM dd"));
  426. ----
  427. [classname]#LocalDateTimeRenderer#::
  428. Otherwise the same as [classname]#LocalDateRenderer#, except for the [classname]#LocalDateTime# type.
  429. +
  430. [source, java]
  431. ----
  432. LocalDateTimeRenderer renderer = new LocalDateTimeRenderer(
  433. () -> DateTimeFormatter
  434. .ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT)
  435. .withLocale(Locale.ENGLISH));
  436. Column<Person, LocalDateTime> bornColumn =
  437. grid.addColumn(Person::getBirthDateAndTime, renderer);
  438. // Alternatively, with a custom pattern:
  439. Column<Person, LocalDateTime> bornColumn =
  440. grid.addColumn(
  441. Person::getBirthDateAndTime,
  442. new LocalDateTimeRenderer("yyyy.MM.dd 'at' hh:mm"));
  443. ----
  444. [classname]#ComponentRenderer#:: Renders a Vaadin [classname]#Component# in a column. Since components
  445. are quite complex, the [classname]#ComponentRenderer# comes with possible performance issues.
  446. To use it efficiently you should use as few nested components as possible. If the components used are
  447. of a different size than the default row height, [methodname]#Grid.setBodyRowHeight()# can be used to adjust
  448. the height of all rows in the Grid.
  449. +
  450. Use [classname]#Button# in [classname]#Grid#:
  451. +
  452. [source, java]
  453. ----
  454. grid.addComponentColumn(person -> {
  455. Button button = new Button("Click me!");
  456. button.addClickListener(click ->
  457. Notification.show("Clicked: " + person.toString()));
  458. return button;
  459. });
  460. // make sure the buttons fit in the cells of the Grid
  461. grid.setBodyRowHeight(40);
  462. ----
  463. +
  464. Components will occasionally be generated again during runtime. If you have a state in your
  465. component and not in the data object, you need to handle storing it yourself. Below is a simple
  466. example on how to achieve this.
  467. +
  468. Store a [classname]#TextField# with changed value.
  469. +
  470. [source, java]
  471. ----
  472. Map<Person, TextField> textFields = new HashMap<>();
  473. grid.addColumn(person -> {
  474. // Check for existing text field
  475. if (textFields.containsKey(person)) {
  476. return textFields.get(person);
  477. }
  478. // Create a new one
  479. TextField textField = new TextField();
  480. textField.setValue(person.getLastname());
  481. // Store the text field when user updates the value
  482. textField.addValueChangeListener(change ->
  483. textFields.put(person, textField));
  484. return textField;
  485. }, new ComponentRenderer());
  486. ----
  487. [classname]#Components# in [classname]#Grid# [classname]#ComponentRenderer# are wrapped in a [literal]#++div++# with the style name [literal]#++component-wrap++#. This can be used to style the alignment and size of the [classname]#Component#.
  488. [[components.grid.renderer.custom]]
  489. === Custom Renderers
  490. Renderers are component extensions that require a client-side counterpart. See
  491. <<../clientsidewidgets/clientsidewidgets-grid#clientsidewidgets.grid.renderers,"Renderers">>
  492. for information on implementing custom renderers.
  493. [[components.grid.headerfooter]]
  494. == Header and Footer
  495. A grid by default has a header, which displays column names, and can have a
  496. footer. Both can have multiple rows and neighbouring header row cells can be
  497. joined to feature column groups.
  498. [[components.grid.headerfooter.adding]]
  499. === Adding and Removing Header and Footer Rows
  500. A new header row is added with [methodname]#prependHeaderRow()#, which adds it
  501. at the top of the header, [methodname]#appendHeaderRow()#, which adds it at the
  502. bottom of the header, or with [methodname]#addHeaderRowAt()#, which inserts it
  503. at the specified 0-base index. All of the methods return a
  504. [classname]#HeaderRow# object, which you can use to work on the header further.
  505. [source, java]
  506. ----
  507. // Group headers by joining the cells
  508. HeaderRow groupingHeader = grid.prependHeaderRow();
  509. ...
  510. // Create a header row to hold column filters
  511. HeaderRow filterRow = grid.appendHeaderRow();
  512. ...
  513. ----
  514. Similarly, you can add footer rows with [methodname]#appendFooterRow()#,
  515. [methodname]#prependFooterRow()#, and [methodname]#addFooterRowAt()#.
  516. [[components.grid.headerfooter.joining]]
  517. === Joining Header and Footer Cells
  518. You can join two or more header or footer cells with the [methodname]#join()#
  519. method. For header cells, the intention is usually to create column grouping,
  520. while for footer cells, you typically calculate sums or averages.
  521. [source, java]
  522. ----
  523. // Group headers by joining the cells
  524. HeaderRow groupingHeader = grid.prependHeaderRow();
  525. HeaderCell namesCell = groupingHeader.join(
  526. groupingHeader.getCell("firstname"),
  527. groupingHeader.getCell("lastname")).setText("Person");
  528. HeaderCell yearsCell = groupingHeader.join(
  529. groupingHeader.getCell("born"),
  530. groupingHeader.getCell("died"),
  531. groupingHeader.getCell("lived")).setText("Dates of Life");
  532. ----
  533. [[components.grid.headerfooter.content]]
  534. === Text and HTML Content
  535. You can set the header caption with [methodname]#setText()#, in which case any
  536. HTML formatting characters are quoted to ensure security.
  537. [source, java]
  538. ----
  539. HeaderRow mainHeader = grid.getDefaultHeaderRow();
  540. mainHeader.getCell("firstname").setText("First Name");
  541. mainHeader.getCell("lastname").setText("Last Name");
  542. mainHeader.getCell("born").setText("Born In");
  543. mainHeader.getCell("died").setText("Died In");
  544. mainHeader.getCell("lived").setText("Lived For");
  545. ----
  546. To use raw HTML in the captions, you can use [methodname]#setHtml()#.
  547. [source, java]
  548. ----
  549. namesCell.setHtml("<b>Names</b>");
  550. yearsCell.setHtml("<b>Years</b>");
  551. ----
  552. [[components.grid.headerfooter.components]]
  553. === Components in Header or Footer
  554. You can set a component in a header or footer cell with
  555. [methodname]#setComponent()#. Often, this feature is used to allow filtering.
  556. [NOTE]
  557. ====
  558. Note, when you use [methodname]#setComponent(TextField)#, the [classname]#TextField# will be rendered in compact mode
  559. without caption and icon. If you need to override this behavior, you need to wrap the [classname]#TextField# e.g.
  560. into [classname]#HorizontalLayout#.
  561. ====
  562. ////
  563. // commented out until filtering is sorted for 8
  564. [[components.grid.filtering]]
  565. == Filtering
  566. The ability to include components in the grid header can be used to create
  567. filters for the grid data. Filtering is done in the container data source, so
  568. the container must be of type that implements
  569. [interfacename]#Container.Filterable#.
  570. [[figure.components.grid.filtering]]
  571. .Filtering Grid
  572. image::img/grid-filtering.png[width=50%, scaledwidth=80%]
  573. The filtering illustrated in <<figure.components.grid.filtering>> can be created
  574. as follows:
  575. [source, java]
  576. ----
  577. // Have a list of persons
  578. List<Person> people = getPeople();
  579. // Create a grid bound to it
  580. Grid<Person> grid = new Grid<>();
  581. grid.setItems(people);
  582. grid.setSelectionMode(SelectionMode.NONE);
  583. grid.setWidth("500px");
  584. grid.setHeight("300px");
  585. // Create a header row to hold column filters
  586. HeaderRow filterRow = grid.appendHeaderRow();
  587. // Set up a filter for all columns
  588. for (Column<?> col: grid.getColumns()) {
  589. HeaderCell cell = filterRow.getCell(col);
  590. // Have an input field to use for filter
  591. TextField filterField = new TextField();
  592. // Update filter When the filter input is changed
  593. filterField.addValueChangeListener(event -> {
  594. // Filter the list of items
  595. List<String> filteredList =
  596. // XXX shouldn't use Lists here since it's from Guava instead of the vanilla JRE. Revise when updating this code example for the new filtering API!
  597. Lists.newArrayList(personList.filter(persons,
  598. Predicates.containsPattern(event.getValue())));
  599. // Apply filtered data
  600. grid.setItems(filteredList);
  601. });
  602. cell.setComponent(filterField);
  603. }
  604. ----
  605. ////
  606. [[components.grid.sorting]]
  607. == Sorting
  608. A user can sort the data in a grid on a column by clicking the column header.
  609. Clicking another time on the current sort column reverses the sort direction.
  610. Clicking on other column headers while keeping the Shift key pressed adds a
  611. secondary or more sort criteria.
  612. [[figure.components.grid.sorting]]
  613. .Sorting Grid on Multiple Columns
  614. image::img/grid-sorting.png[width=50%, scaledwidth=75%]
  615. Defining sort criteria programmatically can be done with the various
  616. alternatives of the [methodname]#sort()# method. You can sort on a specific
  617. column with [methodname]#sort(Column column)#, which defaults to ascending
  618. sorting order, or [methodname]#sort(Column column, SortDirection
  619. direction)#, which allows specifying the sort direction.
  620. [source, java]
  621. ----
  622. grid.sort(nameColumn, SortDirection.DESCENDING);
  623. ----
  624. To sort by multiple columns, you need to use the fluid sort builder API
  625. [classname]#GridSortOrderBuilder#, which allows you to easily construct sorting information to be passed to grid's [methodname]#setSortOrder()# method.
  626. A sort builder is created with the static methods [methodname]#asc()# and [methodname]#desc()#,
  627. and additional sorting information can by chained with [methodname]#thenAsc()# and [methodname]#thenDesc()#.
  628. [source, java]
  629. ----
  630. // Sort first by city (ascending) and then by name (descending)
  631. grid.setSortOrder(GridSortOrder.asc(cityColumn).thenDesc(nameColumn))
  632. ----
  633. [[components.grid.editing]]
  634. == Editing Items Inside Grid
  635. Grid supports line-based editing, where double-clicking a row opens the row
  636. editor. In the editor, the input fields can be edited, as well as navigated with
  637. kbd:[Tab] and kbd:[Shift+Tab] keys. If validation fails, an error is displayed and the user
  638. can correct the inputs.
  639. The [classname]#Editor# is accessible via [methodname]#getEditor()#, and to enable editing, you need to call [methodname]#setEnabled(true)# on it.
  640. The editor is based on [classname]#Binder# which is used to bind the data to the editor.
  641. See <<../datamodel/datamodel-forms.asciidoc#datamodel.forms.beans,"Binding Beans to Forms">> for more information on setting up field components and validation by using [classname]#Binder#.
  642. For each column that should be editable, a binding should be created in the editor binder and then the column is configured to use that binding.
  643. For simple cases where no conversion or validation is needed, it is also possible to directly use `setEditorComponent` on a `Column` to only define the editor component and a setter that updates the row object when saving.
  644. [source, java]
  645. ----
  646. List<Todo> items = Arrays.asList(new Todo("Done task", true),
  647. new Todo("Not done", false));
  648. Grid<Todo> grid = new Grid<>();
  649. TextField taskField = new TextField();
  650. CheckBox doneField = new CheckBox();
  651. Binder<Todo> binder = grid.getEditor().getBinder();
  652. Binding<Todo, Boolean> doneBinding = binder.bind(
  653. doneField, Todo::isDone, Todo::setDone);
  654. Column<Todo, String> column = grid.addColumn(
  655. todo -> String.valueOf(todo.isDone()));
  656. column.setWidth(75);
  657. column.setEditorBinding(doneBinding);
  658. grid.addColumn(Todo::getTask).setEditorComponent(
  659. taskField, Todo::setTask).setExpandRatio(1);
  660. grid.getEditor().setEnabled(true);
  661. ----
  662. [[components.grid.editing.buffered]]
  663. === Buffered / Unbuffered Mode
  664. Grid supports two editor modes - buffered and unbuffered. The default mode is
  665. buffered. The mode can be changed with [methodname]#setBuffered(false)#.
  666. In the buffered mode, editor has two buttons visible: a [guibutton]#Save# button that commits
  667. the modifications to the bean and closes the editor and a [guibutton]#Cancel# button
  668. discards the changes and exits the editor.
  669. Editor in buffered mode is illustrated in <<figure.components.grid.editing>>.
  670. [[figure.components.grid.editing]]
  671. .Editing a Grid Row
  672. image::img/grid-editor-basic.png[width=50%, scaledwidth=75%]
  673. In the unbuffered mode, the editor has no buttons and all changed data is committed directly
  674. to the data provider. If another row is clicked, the editor for the current row is closed and
  675. a row editor for the clicked row is opened.
  676. [[components.grid.editing.captions]]
  677. === Customizing Editor Buttons
  678. In the buffered mode, the editor has two buttons: [guibutton]#Save# and [guibutton]#Cancel#. You can
  679. set their captions with [methodname]#setEditorSaveCaption()# and
  680. [methodname]#setEditorCancelCaption()#, respectively.
  681. In the following example, we demonstrate one way to translate the captions:
  682. [source, java]
  683. ----
  684. // Localize the editor button captions
  685. grid.getEditor().setSaveCaption("Tallenna");
  686. grid.getEditor().setCancelCaption("Peruuta"));
  687. ----
  688. [[components.grid.editing.validation]]
  689. === Handling Validation Errors
  690. The input fields are validated when the value is updated. The default
  691. error handler displays error indicators in the invalid fields, as well as the
  692. first error in the editor.
  693. [[figure.components.grid.errors]]
  694. .Editing a Grid Row
  695. image::img/grid-editor-errors.png[width=50%, scaledwidth=75%]
  696. You can modify the error message by implementing a custom
  697. [interfacename]#EditorErrorGenerator# with for the [classname]#Editor#.
  698. [[components.grid.presentation.provider]]
  699. === Presentation Value Providers
  700. By default, a renderer displays the column value. If you want to edit an
  701. internal value (such as an address object) but show a simpler representation
  702. when not editing a row, a presentation value provider can be used.
  703. A presentation value provider converts the value of a cell (obtained with a
  704. value provider, and used by the editor) to a different representation to be
  705. shown by renderers when the cell is not being edited. A custom renderer can
  706. optionally be used for the presentation values.
  707. In the following example, we demonstrate one way to use a simplified
  708. presentation of an address column while allowing editing the full address:
  709. [source, java]
  710. ----
  711. Column<Person, Address> column = grid.addColumn(Person::getAddress);
  712. // alternatively, the presentation provider can be given as an extra parameter
  713. // to addColumn()
  714. column.setRenderer(
  715. address -> address.getCity() + " " + address.getCountry(),
  716. new TextRenderer());
  717. column.setCaption("Address");
  718. column.setEditorComponent(new AddressField(), Person::setAddress);
  719. ----
  720. ////
  721. // Not supported in 8
  722. [[components.grid.scrolling]]
  723. == Programmatic Scrolling
  724. You can scroll to first item with [methodname]#scrollToStart()#, to end with
  725. [methodname]#scrollToEnd()#, or to a specific row with [methodname]#scrollTo()#.
  726. ////
  727. == Drag and Drop of Rows
  728. Please refer to the
  729. <<../advanced/advanced-dragndrop#advanced.dragndrop.grid,"Drag and Drop Rows in Grid">> documentation.
  730. [[advanced.dragndrop.grid]]
  731. [[components.grid.stylegeneration]]
  732. == Generating Row or Cell Styles
  733. You can style entire rows or individual cells with a
  734. [interfacename]#StyleGenerator#, typically used through Java lambdas.
  735. [[components.grid.stylegeneration.row]]
  736. === Generating Row Styles
  737. The easiest way to style rows is to make a lambda and set it with
  738. [methodname]#setStyleGenerator()# to a grid.
  739. The lambda gets a data item, and should return a style name or [parameter]#null# if
  740. no style is generated.
  741. For example, to add a style names to rows having certain values in one
  742. property of an item, you can style them as follows:
  743. [source, java]
  744. ----
  745. grid.setStyleGenerator(person ->
  746. // Style based on alive status
  747. person.isAlive() ? null : "dead"
  748. );
  749. ----
  750. You could then style the rows with CSS as follows:
  751. [source, css]
  752. ----
  753. .v-grid-row.dead {
  754. color: gray;
  755. }
  756. ----
  757. [[components.grid.stylegeneration.cell]]
  758. === Generating Cell Styles
  759. You set a [interfacename]#StyleGenerator# to a grid with
  760. [methodname]#setStyleGenerator()#. The [methodname]#getStyle()# method gets
  761. a [classname]#CellReference#, which contains various information about the cell
  762. and a reference to the grid, and should return a style name or [parameter]#null#
  763. if no style is generated.
  764. For example, to add a style name to a specific column, you can match on
  765. the column as follows:
  766. [source, java]
  767. ----
  768. // Static style based on column
  769. bornColumn.setStyleGenerator(person -> "rightalign");
  770. ----
  771. You could then style the cells with a CSS rule as follows:
  772. [source, css]
  773. ----
  774. .v-grid-cell.rightalign {
  775. text-align: right;
  776. }
  777. ----
  778. [[components.grid.css]]
  779. == Styling with CSS
  780. [source, css]
  781. ----
  782. .v-grid {
  783. .v-grid-scroller, .v-grid-scroller-horizontal { }
  784. .v-grid-tablewrapper {
  785. .v-grid-header {
  786. .v-grid-row {
  787. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  788. }
  789. }
  790. .v-grid-body {
  791. .v-grid-row,
  792. .v-grid-row-stripe,
  793. .v-grid-row-has-data {
  794. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  795. }
  796. }
  797. .v-grid-footer {
  798. .v-grid-row {
  799. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  800. }
  801. }
  802. }
  803. .v-grid-header-deco { }
  804. .v-grid-footer-deco { }
  805. .v-grid-horizontal-scrollbar-deco { }
  806. .v-grid-editor {
  807. .v-grid-editor-cells { }
  808. .v-grid-editor-footer {
  809. .v-grid-editor-message { }
  810. .v-grid-editor-buttons {
  811. .v-grid-editor-save { }
  812. .v-grid-editor-cancel { }
  813. }
  814. }
  815. }
  816. }
  817. ----
  818. A [classname]#Grid# has an overall [literal]#++v-grid++# style. The actual grid
  819. has three parts: a header, a body, and a footer. The scrollbar is a custom
  820. element with [literal]#++v-grid-scroller++# style. In addition, there are some
  821. decoration elements.
  822. Grid cells, whether thay are in the header, body, or footer, have a basic
  823. [literal]#++v-grid-cell++# style. Cells in a frozen column additionally have a
  824. [literal]#++frozen++# style. Rows have [literal]#++v-grid-row++# style, and
  825. every other row has additionally a [literal]#++v-grid-row-stripe++# style.
  826. The focused row has additionally [literal]#++v-grid-row-focused++# style and
  827. focused cell [literal]#++v-grid-cell-focused++#. By default, cell focus is
  828. visible, with the border stylable with [parameter]#$v-grid-cell-focused-border#
  829. parameter in Sass. Row focus has no visible styling, but can be made visible
  830. with the [parameter]#$v-grid-row-focused-background-color# parameter or with a
  831. custom style rule.
  832. In editing mode, a [literal]#++v-grid-editor++# overlay is placed on the row
  833. under editing. In addition to the editor field cells, it has an error message
  834. element, as well as the buttons.
  835. ((()))