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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. ---
  2. title: Grid
  3. order: 24
  4. layout: page
  5. ---
  6. [[components.grid]]
  7. = [classname]#Grid#
  8. *_This section has not yet been updated for Vaadin Framework 8_*
  9. ifdef::web[]
  10. [.sampler]
  11. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/grids-and-trees/grid"]
  12. endif::web[]
  13. [[components.grid.overview]]
  14. == Overview
  15. [classname]#Grid# is for displaying and editing tabular data laid out in rows
  16. and columns. At the top, a __header__ can be shown, and a __footer__ at the
  17. bottom. In addition to plain text, the header and footer can contain HTML and
  18. components. Having components in the header allows implementing filtering
  19. easily. The grid data can be sorted by clicking on a column header;
  20. shift-clicking a column header enables secondary sorting criteria.
  21. [[figure.components.grid.features]]
  22. .A [classname]#Grid#
  23. image::img/grid-features.png[width=70%, scaledwidth=100%]
  24. The data area can be scrolled both vertically and horizontally. The leftmost
  25. columns can be frozen, so that they are never scrolled out of the view. The data
  26. is loaded lazily from the server, so that only the visible data is loaded. The
  27. smart lazy loading functionality gives excellent user experience even with low
  28. bandwidth, such as mobile devices.
  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. Finally, [classname]#Grid# is designed to be extensible and used just as well
  35. for client-side development - its GWT API is nearly identical to the server-side
  36. API, including data binding.
  37. [[components.grid.data]]
  38. == Binding to Data
  39. [classname]#Grid# is normally used by binding it to a container data source,
  40. described in
  41. <<dummy/../../../framework/datamodel/datamodel-overview.asciidoc#datamodel.overview,"Binding Components to Data">>.
  42. By default, it is bound to List of items. You can set the items in the constructor or with
  43. [methodname]#setItems()# method.
  44. For example, if you have a list of beans, you could add to a [classname]#Grid# as follows
  45. [source, java]
  46. ----
  47. // Have some data
  48. List<Person> people = Lists.newArrayList(
  49. new Person("Nicolaus Copernicus", 1543),
  50. new Person("Galileo Galilei", 1564),
  51. new Person("Johannes Kepler", 1571));
  52. // Create a grid bound to the list
  53. Grid<Person> grid = new Grid<>(people);
  54. grid.addColumn("Name", Person::getName);
  55. grid.addColumn("Year of birth", Person::getBirthYear);
  56. layout.addComponent(grid);
  57. ----
  58. In addition to list you can pass items individually:
  59. [source, java]
  60. ----
  61. grid.setItems(new Person("Nicolaus Copernicus", 1543),
  62. new Person("Galileo Galilei", 1564));
  63. ----
  64. Note that you can not use [methodname]#addRow()# to add items if the container
  65. is read-only or has read-only columns, such as generated columns.
  66. [[components.grid.selection]]
  67. == Handling Selection Changes
  68. Selection in [classname]#Grid# is handled a bit differently from other selection
  69. components, as it is not an [classname]#AbstractSelect#. Grid supports both
  70. single and multiple selection, defined by the __selection model__. Selection
  71. events can be handled with a [interfacename]#SelectionListener#.
  72. [[components.grid.selection.mode]]
  73. === Selection Models
  74. A [classname]#Grid# can be set to be in [literal]#++SINGLE++# (default),
  75. [literal]#++MULTI++#, or [literal]#++NONE++# selection mode, defined in the
  76. [interfacename]#SelectionMode# enum.
  77. [source, java]
  78. ----
  79. // Use single-selection mode (default)
  80. grid.setSelectionMode(SelectionMode.SINGLE);
  81. ----
  82. Empty (null) selection is allowed by default, but can be disabled
  83. with [methodname]#setDeselectAllowed()# in single-selection mode.
  84. [source, java]
  85. ----
  86. // Pre-select 3rd item from the person list
  87. grid.select(personList.get(2));
  88. ----
  89. [[components.grid.selection.single]]
  90. === Handling Selection
  91. Changes in the selection can be handled with a
  92. [interfacename]#SelectionListener#. You need to implement the
  93. [methodname]#select()# method, which gets a [classname]#SelectionEvent# as
  94. parameter. In addition to selection, you can handle clicks on rows or cells with
  95. a [interfacename]#CellClickListener#.
  96. You can get the new selection from the selection event with
  97. [methodname]#getSelected()#, which returns a set of items, or more simply
  98. from the grid.
  99. For example:
  100. [source, java]
  101. ----
  102. grid.addSelectionListener(selectionEvent -> {
  103. // Get selection from the selection model
  104. Collection<Person> selectedPersons =
  105. selectionEvent.getSelected();
  106. if (!selectedPersons.isEmpty())
  107. Notification.show("Selected " + selectedPersons);
  108. else
  109. Notification.show("Nothing selected");
  110. });
  111. ----
  112. The current selection can be obtained from the [classname]#Grid# object by
  113. [methodname]#getSelectedItem()# or [methodname]#getSelectedItems()#, which return
  114. one (in single-selection mode) or all (in multi-selection mode) selected items.
  115. [WARNING]
  116. ====
  117. If you change the data source for a grid, it will clear the selection. To keep
  118. the previous selection you must reset the selection afterwards using the
  119. [methodname]#select()# method.
  120. ====
  121. [[components.grid.selection.multi]]
  122. === Multiple Selection
  123. In the multiple selection mode, a user can select multiple items by clicking on
  124. the checkboxes in the leftmost column, or by using the kbd:[Space] to select/deselect the currently focused row.
  125. Space bar is the default key for toggling the selection, but it can be customized.
  126. [[figure.components.grid.selection.multi]]
  127. .Multiple Selection in [classname]#Grid#
  128. image::img/grid-selection-multi.png[width=50%, scaledwidth=75%]
  129. You can use [methodname]#select()# to add items to the selection.
  130. [source, java]
  131. ----
  132. // Grid in multi-selection mode
  133. Grid<Person> grid = Grid<>(personList)
  134. grid.setSelectionMode(SelectionMode.MULTI);
  135. // Items 2-4
  136. personList.subList(2,3).forEach(grid::select);
  137. ----
  138. The current selection can be read with [methodname]#getSelected()#
  139. in the [classname]#Grid#.
  140. [source, java]
  141. ----
  142. // Allow deleting the selected items
  143. Button delSelected = new Button("Delete Selected", e -> {
  144. // Delete all selected data items
  145. for (Person person: selection.getSelected())
  146. personList.remove(person);
  147. // Disable after deleting
  148. e.getButton().setEnabled(false);
  149. // Reset grid content from the list
  150. grid.setItems(personList);
  151. });
  152. delSelected.setEnabled(!grid.getSelected().isEmpty());
  153. ----
  154. Changes in the selection can be handled with a
  155. [interfacename]#SelectionListener#. The selection event object provides
  156. [methodname]#getAdded()# and [methodname]#getRemoved()# to allow determining the
  157. differences in the selection change.
  158. [source, java]
  159. ----
  160. // Handle selection changes
  161. grid.addSelectionListener(selection -> { // Java 8
  162. Notification.show(selection.getAdded().size() +
  163. " items added, " +
  164. selection.getRemoved().size() +
  165. " removed.");
  166. // Allow deleting only if there's any selected
  167. deleteSelected.setEnabled(
  168. grid.getSelectedRows().size() > 0);
  169. });
  170. ----
  171. [[components.grid.selection.clicks]]
  172. === Focus and Clicks
  173. In addition to selecting rows, you can focus individual cells. The focus can be
  174. moved with arrow keys and, if editing is enabled, pressing kbd:[Enter] opens the
  175. editor. Normally, pressing kbd:[Tab] or kbd:[Shift+Tab] moves the focus to another component,
  176. as usual.
  177. When editing or in unbuffered mode, kbd:[Tab] or kbd:[Shift+Tab] moves the focus to the next or
  178. previous cell. The focus moves from the last cell of a row forward to the
  179. beginning of the next row, and likewise, from the first cell backward to the
  180. end of the previous row. Note that you can extend [classname]#DefaultEditorEventHandler#
  181. to change this behavior.
  182. With the mouse, you can focus a cell by clicking on it. The clicks can be handled
  183. with an [interfacename]#ItemClickListener#. The [classname]#ItemClickEvent#
  184. object contains various information, most importantly the ID of the clicked row
  185. and column.
  186. [source, java]
  187. ----
  188. grid.addCellClickListener(event ->
  189. Notification.show("Value: " + event.getItem());
  190. ----
  191. The clicked grid cell is also automatically focused.
  192. The focus indication is themed so that the focused cell has a visible focus
  193. indicator style by default, while the row does not. You can enable row focus, as
  194. well as disable cell focus, in a custom theme. See <<components.grid.css>>.
  195. [[components.grid.columns]]
  196. == Configuring Columns
  197. Columns are normally defined in the container data source. The
  198. [methodname]#addColumn()# method can be used to add columns to [classname]#Grid#.
  199. Column configuration is defined in [classname]#Grid.Column# objects, which can
  200. be obtained from the grid with [methodname]#getColumns()#.
  201. [source, java]
  202. ----
  203. Column<Date> bornColumn = grid.addColumn(Person:getBirthDate);
  204. bornColumn.setHeaderCaption("Born date");
  205. ----
  206. In the following, we describe the basic column configuration.
  207. [[components.grid.columns.order]]
  208. === Column Order
  209. You can set the order of columns with [methodname]#setColumnOrder()# for the
  210. grid. Columns that are not given for the method are placed after the specified
  211. columns in their natural order.
  212. [source, java]
  213. ----
  214. grid.setColumnOrder(firstnameColumn, lastnameColumn,
  215. bornColumn, birthplaceColumn,
  216. diedColumn);
  217. ----
  218. Note that the method can not be used to hide columns. You can hide columns with
  219. the [methodname]#removeColumn()#, as described later.
  220. [[components.grid.columns.removing]]
  221. === Hiding and Removing Columns
  222. Columns can be hidden by calling [methodname]#setHidden()# in [classname]#Column#.
  223. Furthermore, you can set the columns user hideable using method
  224. [methodname]#setHideable()#.
  225. Columns can be removed with [methodname]#removeColumn()# and
  226. [methodname]#removeAllColumns()#. To restore a previously removed column,
  227. you can call [methodname]#addColumn()#.
  228. [[components.grid.columns.captions]]
  229. === Column Captions
  230. Column captions are displayed in the grid header. You can set the header caption
  231. explicitly through the column object with [methodname]#setHeaderCaption()#.
  232. [source, java]
  233. ----
  234. Column<Date> bornColumn = grid.addColumn(Person:getBirthDate);
  235. bornColumn.setHeaderCaption("Born date");
  236. ----
  237. This is equivalent to setting it with [methodname]#setText()# for the header
  238. cell; the [classname]#HeaderCell# also allows setting the caption in HTML or as
  239. a component, as well as styling it, as described later in
  240. <<components.grid.headerfooter>>.
  241. [[components.grid.columns.width]]
  242. === Column Widths
  243. Columns have by default undefined width, which causes automatic sizing based on
  244. the widths of the displayed data. You can set column widths explicitly by pixel
  245. value with [methodname]#setWidth()#, or relatively using expand ratios with
  246. [methodname]#setExpandRatio()#.
  247. When using expand ratios, the columns with a non-zero expand ratio use the extra
  248. space remaining from other columns, in proportion to the defined ratios.
  249. You can specify minimum and maximum widths for the expanding columns with
  250. [methodname]#setMinimumWidth()# and [methodname]#setMaximumWidth()#,
  251. respectively.
  252. The user can resize columns by dragging their separators with the mouse. When resized manually,
  253. all the columns widths are set to explicit pixel values, even if they had
  254. relative values before.
  255. [[components.grid.columns.frozen]]
  256. === Frozen Columns
  257. You can set the number of columns to be frozen with
  258. [methodname]#setFrozenColumnCount()#, so that they are not scrolled off when
  259. scrolling horizontally.
  260. [source, java]
  261. ----
  262. grid.setFrozenColumnCount(2);
  263. ----
  264. Setting the count to [parameter]#0# disables frozen data columns; setting it to
  265. [parameter]#-1# also disables the selection column in multi-selection mode.
  266. [[components.grid.generatedcolumns]]
  267. == Generating Columns
  268. Columns with values computed from other columns can be simply added by using
  269. lambdas:
  270. [source, java]
  271. ----
  272. // Add generated full name column
  273. Column<String> fullNameColumn = grid.addColumn(person ->
  274. person.getFirstName() + " " + person.getLastName());
  275. fullNameColumn.setHeaderCaption("Full name");
  276. ----
  277. [[components.grid.renderer]]
  278. == Column Renderers
  279. A __renderer__ is a feature that draws the client-side representation of a data
  280. value. This allows having images, HTML, and buttons in grid cells.
  281. [[figure.components.grid.renderer]]
  282. .Column renderers: image, date, HTML, and button
  283. image::img/grid-renderers.png[width=75%, scaledwidth=100%]
  284. Renderers implement the [interfacename]#Renderer# interface.
  285. You set the column renderer in the [classname]#Grid.Column# object as follows:
  286. [source, java]
  287. ----
  288. Column<Integer> bornColumn = grid.addColumn(Person:getBirthYear);
  289. ...
  290. Grid.Column bornColumn = grid.getColumn("born");
  291. bornColumn.setRenderer(new NumberRenderer("born in %d AD"));
  292. ----
  293. Renderers require a specific data type for the column. To convert to a property
  294. type to a type required by a renderer, you can pass an optional
  295. [interfacename]#Converter# to [methodname]#setRenderer()#, as described later in
  296. this section. A converter can also be used to (pre)format the property values.
  297. The converter is run on the server-side, before sending the values to the
  298. client-side to be rendered with the renderer.
  299. The following renderers are available, as defined in the server-side
  300. [package]#com.vaadin.ui.renderers# package:
  301. [classname]#ButtonRenderer#:: Renders the data value as the caption of a button. A [interfacename]#RendererClickListener# can be given to handle the button clicks.
  302. ifdef::web[]
  303. +
  304. Typically, a button renderer is used to display buttons for operating on a data
  305. item, such as edit, view, delete, etc. It is not meaningful to store the button
  306. captions in the data source, rather you want to generate them, and they are
  307. usually all identical.
  308. +
  309. [source, java]
  310. ----
  311. List<Person> people = new ArrayList<>();
  312. people.add(new Person("Nicolaus Copernicus", 1473));
  313. people.add(new Person("Galileo Galilei", 1564));
  314. people.add(new Person("Johannes Kepler", 1571));
  315. // Create a grid
  316. Grid<Person> grid = new Grid(people);
  317. // Render a button that deletes the data row (item)
  318. grid.addColumn(person -> "Delete" )
  319. .setRenderer(new ButtonRenderer(clickEvent -> {
  320. people.remove(clickEvent.getValue());
  321. grid.setItems(people);
  322. });
  323. ----
  324. endif::web[]
  325. [classname]#ImageRenderer#:: Renders the cell as an image.
  326. The column type must be a [interfacename]#Resource#, as described in
  327. <<dummy/../../../framework/application/application-resources#application.resources,"Images and Other Resources">>; only [classname]#ThemeResource# and
  328. [classname]#ExternalResource# are currently supported for images in
  329. [classname]#Grid#.
  330. ifdef::web[]
  331. +
  332. [source, java]
  333. ----
  334. Column<ThemeResource> imageColumn = grid.addColumn("picture",
  335. p -> new ThemeResource("img/"+p.getLastname()+".jpg"));
  336. imageColumn.setRenderer(new ImageRenderer());
  337. ----
  338. +
  339. You also need to define the row heights so that the images fit there. You can
  340. set it in the theme for all data cells or for the column containing the images.
  341. +
  342. For the latter way, first define a CSS style name for grid and the column:
  343. +
  344. [source, java]
  345. ----
  346. grid.setStyleName("gridwithpics128px");
  347. imageColumn.setCellStyleGenerator(cell -> "imagecol");
  348. ----
  349. ifdef::web[]
  350. +
  351. Then, define the style in CSS (Sass):
  352. endif::web[]
  353. +
  354. [source, css]
  355. ----
  356. .gridwithpics128px .imagecol {
  357. height: 128px;
  358. background: black;
  359. text-align: center;
  360. }
  361. ----
  362. endif::web[]
  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. ifdef::web[]
  368. +
  369. [source, java]
  370. ----
  371. Grid.Column<Date> bornColumn = grid.addColumn(person:getBirthDate);
  372. bornColumn.setRenderer(
  373. new DateRenderer("%1$tB %1$te, %1$tY",
  374. Locale.ENGLISH));
  375. ----
  376. +
  377. Optionally, a locale can be given. Otherwise, the default locale (in the
  378. component tree) is used.
  379. endif::web[]
  380. [classname]#HTMLRenderer#:: Renders the cell as HTML.
  381. This allows formatting the cell content, as well as using HTML features such as hyperlinks.
  382. ifdef::web[]
  383. +
  384. Set the renderer in the [classname]#Grid.Column# object:
  385. +
  386. [source, java]
  387. ----
  388. Column<String> htmlColumn grid.addColumn(person ->
  389. "<a href='" + person.getDetailsUrl() + "' target='_top'>info</a>");
  390. htmlColumn.setRenderer(new HtmlRenderer());
  391. ----
  392. endif::web[]
  393. [classname]#NumberRenderer#:: Formats column values with a numeric type extending [classname]#Number#:
  394. [classname]#Integer#, [classname]#Double#, etc. The format can be specified
  395. either by the subclasses of [classname]#java.text.NumberFormat#, namely
  396. [classname]#DecimalFormat# and [classname]#ChoiceFormat#, or by
  397. [methodname]#String.format()#.
  398. ifdef::web[]
  399. +
  400. For example:
  401. +
  402. [source, java]
  403. ----
  404. // Define some columns
  405. Column<String> nameCol = grid.addColumn(person::getName);
  406. Column<Integer> bornCol = grid.addColumn(person:getBirthYear);
  407. Column<Integer> slettersCol = grid.addColumn("sletters");
  408. Column<Double> ratingCol = grid.addColumn("rating");
  409. // Use decimal format
  410. bornCol.setRenderer(new NumberRenderer(
  411. new DecimalFormat("in #### AD")));
  412. // Use textual formatting on numeric ranges
  413. slettersCol.setRenderer(new NumberRenderer(
  414. new ChoiceFormat("0#none|1#one|2#multiple")));
  415. // Use String.format() formatting
  416. ratingCol.setRenderer(new NumberRenderer(
  417. "%02.4f", Locale.ENGLISH));
  418. // Add some data rows
  419. grid.addItems(new Person("Nicolaus Copernicus", 1473, 2, 0.4),
  420. new Person("Galileo Galilei", 1564, 0, 4.2),
  421. new Person("Johannes Kepler", 1571, 1, 2.3));
  422. ----
  423. endif::web[]
  424. [classname]#ProgressBarRenderer#:: Renders a progress bar in a column with a [classname]#Double# type. The value
  425. must be between 0.0 and 1.0.
  426. ifdef::web[]
  427. +
  428. For example:
  429. +
  430. [source, java]
  431. ----
  432. // Define some columns
  433. Column<String> nameCol = grid.addColumn(person::getName);
  434. Column<Double> ratingCol = grid.addColumn("rating");
  435. ratingCol.setRenderer(new ProgressBarRenderer());
  436. // Add some data rows
  437. grid.addItems(new Person("Nicolaus Copernicus", 0.4),
  438. new Person("Galileo Galilei", 4.2),
  439. new Person("Johannes Kepler", 2.3));
  440. ----
  441. endif::web[]
  442. [classname]#TextRenderer#:: Displays plain text as is. Any HTML markup is quoted.
  443. [[components.grid.renderer.custom]]
  444. === Custom Renderers
  445. Renderers are component extensions that require a client-side counterpart. See
  446. <<dummy/../../../framework/clientsidewidgets/clientsidewidgets-grid#clientsidewidgets.grid.renderers,"Renderers">>
  447. for information on implementing custom renderers.
  448. [[components.grid.headerfooter]]
  449. == Header and Footer
  450. A grid by default has a header, which displays column names, and can have a
  451. footer. Both can have multiple rows and neighbouring header row cells can be
  452. joined to feature column groups.
  453. [[components.grid.headerfooter.adding]]
  454. === Adding and Removing Header and Footer Rows
  455. A new header row is added with [methodname]#prependHeaderRow()#, which adds it
  456. at the top of the header, [methodname]#appendHeaderRow()#, which adds it at the
  457. bottom of the header, or with [methodname]#addHeaderRowAt()#, which inserts it
  458. at the specified 0-base index. All of the methods return a
  459. [classname]#HeaderRow# object, which you can use to work on the header further.
  460. [source, java]
  461. ----
  462. // Group headers by joining the cells
  463. HeaderRow groupingHeader = grid.prependHeaderRow();
  464. ...
  465. // Create a header row to hold column filters
  466. HeaderRow filterRow = grid.appendHeaderRow();
  467. ...
  468. ----
  469. Similarly, you can add footer rows with [methodname]#appendFooterRow()#,
  470. [methodname]#prependFooterRow()#, and [methodname]#addFooterRowAt()#.
  471. [[components.grid.headerfooter.joining]]
  472. === Joining Header and Footer Cells
  473. You can join two or more header or footer cells with the [methodname]#join()#
  474. method. For header cells, the intention is usually to create column grouping,
  475. while for footer cells, you typically calculate sums or averages.
  476. [source, java]
  477. ----
  478. // Group headers by joining the cells
  479. HeaderRow groupingHeader = grid.prependHeaderRow();
  480. HeaderCell namesCell = groupingHeader.join(
  481. groupingHeader.getCell("firstname"),
  482. groupingHeader.getCell("lastname")).setText("Person");
  483. HeaderCell yearsCell = groupingHeader.join(
  484. groupingHeader.getCell("born"),
  485. groupingHeader.getCell("died"),
  486. groupingHeader.getCell("lived")).setText("Dates of Life");
  487. ----
  488. [[components.grid.headerfooter.content]]
  489. === Text and HTML Content
  490. You can set the header caption with [methodname]#setText()#, in which case any
  491. HTML formatting characters are quoted to ensure security.
  492. [source, java]
  493. ----
  494. HeaderRow mainHeader = grid.getDefaultHeaderRow();
  495. mainHeader.getCell("firstname").setText("First Name");
  496. mainHeader.getCell("lastname").setText("Last Name");
  497. mainHeader.getCell("born").setText("Born In");
  498. mainHeader.getCell("died").setText("Died In");
  499. mainHeader.getCell("lived").setText("Lived For");
  500. ----
  501. To use raw HTML in the captions, you can use [methodname]#setHtml()#.
  502. [source, java]
  503. ----
  504. namesCell.setHtml("<b>Names</b>");
  505. yearsCell.setHtml("<b>Years</b>");
  506. ----
  507. [[components.grid.headerfooter.components]]
  508. === Components in Header or Footer
  509. You can set a component in a header or footer cell with
  510. [methodname]#setComponent()#. Often, this feature is used to allow filtering, as
  511. described in <<components.grid.filtering>>, which also gives an example of the
  512. use.
  513. [[components.grid.filtering]]
  514. == Filtering
  515. The ability to include components in the grid header can be used to create
  516. filters for the grid data. Filtering is done in the container data source, so
  517. the container must be of type that implements
  518. [interfacename]#Container.Filterable#.
  519. [[figure.components.grid.filtering]]
  520. .Filtering Grid
  521. image::img/grid-filtering.png[width=50%, scaledwidth=80%]
  522. The filtering illustrated in <<figure.components.grid.filtering>> can be created
  523. as follows:
  524. [source, java]
  525. ----
  526. // Have a list of persons
  527. List<Person> persons = exampleDataSource();
  528. // Create a grid bound to it
  529. Grid<Person> grid = new Grid(persons);
  530. grid.setSelectionMode(SelectionMode.NONE);
  531. grid.setWidth("500px");
  532. grid.setHeight("300px");
  533. // Create a header row to hold column filters
  534. HeaderRow filterRow = grid.appendHeaderRow();
  535. // Set up a filter for all columns
  536. for (Column<?> col: grid.getColumns()) {
  537. HeaderCell cell = filterRow.getCell(col);
  538. // Have an input field to use for filter
  539. TextField filterField = new TextField();
  540. // Update filter When the filter input is changed
  541. filterField.addValueChangeListener(event -> {
  542. // Filter the list of items
  543. List<String> filteredList =
  544. Lists.newArrayList(personList.filter(persons,
  545. Predicates.containsPattern(event.getValue())));
  546. // Apply filtered data
  547. grid.setItems(filteredList);
  548. });
  549. cell.setComponent(filterField);
  550. }
  551. ----
  552. [[components.grid.sorting]]
  553. == Sorting
  554. A user can sort the data in a grid on a column by clicking the column header.
  555. Clicking another time on the current sort column reverses the sort direction.
  556. Clicking on other column headers while keeping the Shift key pressed adds a
  557. secondary or more sort criteria.
  558. [[figure.components.grid.sorting]]
  559. .Sorting Grid on Multiple Columns
  560. image::img/grid-sorting.png[width=50%, scaledwidth=75%]
  561. Defining sort criteria programmatically can be done with the various
  562. alternatives of the [methodname]#sort()# method. You can sort on a specific
  563. column with [methodname]#sort(Column column)#, which defaults to ascending
  564. sorting order, or [methodname]#sort(Column column, SortDirection
  565. direction)#, which allows specifying the sort direction.
  566. [source, java]
  567. ----
  568. grid.sort(nameColumn, SortDirection.DESCENDING);
  569. ----
  570. To sort on multiple columns, you need to use the fluid sort API with
  571. [methodname]#sort(Sort)#, which allows chaining sorting rules. Sorting rules are
  572. created with the static [methodname]#by()# method, which defines the primary
  573. sort column, and [methodname]#then()#, which can be used to specify any
  574. secondary sort columns. They default to ascending sort order, but the sort
  575. direction can be given with an optional parameter.
  576. [source, java]
  577. ----
  578. // Sort first by city and then by name
  579. grid.sort(Sort.by(cityColumn, SortDirection.ASCENDING)
  580. .then(nameColumn, SortDirection.DESCENDING));
  581. ----
  582. [[components.grid.editing]]
  583. == Editing
  584. Grid supports line-based editing, where double-clicking a row opens the row
  585. editor. In the editor, the input fields can be edited, as well as navigated with
  586. kbd:[Tab] and kbd:[Shift+Tab] keys. If validation fails, an error is displayed and the user
  587. can correct the inputs.
  588. To enable editing, you need to call [methodname]#setEditorEnabled(true)# for the
  589. grid.
  590. [source, java]
  591. ----
  592. Grid<Person> grid = new Grid(persons);
  593. grid.setEditorEnabled(true);
  594. ----
  595. Grid supports two row editor modes - buffered and unbuffered. The default mode is
  596. buffered. The mode can be changed with [methodname]#setBuffered(false)#
  597. [[components.grid.editing.buffered]]
  598. === Buffered Mode
  599. The editor has a [guibutton]#Save# button that commits
  600. the data item to the container data source and closes the editor. The
  601. [guibutton]#Cancel# button discards the changes and exits the editor.
  602. A row under editing is illustrated in <<figure.components.grid.editing>>.
  603. [[figure.components.grid.editing]]
  604. .Editing a Grid Row
  605. image::img/grid-editor-basic.png[width=50%, scaledwidth=75%]
  606. [[components.grid.editing.unbuffered]]
  607. === Unbuffered Mode
  608. The editor has no buttons and all changed data is committed directly
  609. to the container. If another row is clicked, the editor for the current row is closed and
  610. a row editor for the clicked row is opened.
  611. [[components.grid.editing.fields]]
  612. === Editor Fields
  613. The editor fields are configured in [classname]#Column#and bound to
  614. the bean data source with a [classname]#Binder#, which
  615. also handles tasks such as validation, as explained later.
  616. To disable editing in a particular column, you can call
  617. [methodname]#setEditorField()# in the [classname]#Column# object with
  618. [parameter]#null# parameter.
  619. In the following example, we configure a field with validation and styling:
  620. [source, java]
  621. ----
  622. // Create an editor for name
  623. TextField nameEditor = new TextField();
  624. // Custom CSS style
  625. nameEditor.addStyleName("nameeditor");
  626. // Add editor to name column
  627. nameColumn.setEditorField(nameEditor);
  628. ----
  629. Setting an editor field to [parameter]#null# deletes the currently existing
  630. editor field and makes the column non-editable.
  631. ifdef::web[]
  632. [[components.grid.editing.captions]]
  633. === Customizing Editor Buttons
  634. In the buffered mode, the editor has two buttons: [guibutton]#Save# and [guibutton]#Cancel#. You can
  635. set their captions with [methodname]#setEditorSaveCaption()# and
  636. [methodname]#setEditorCancelCaption()#, respectively.
  637. In the following example, we demonstrate one way to translate the captions:
  638. [source, java]
  639. ----
  640. // Captions are stored in a resource bundle
  641. ResourceBundle bundle = ResourceBundle.getBundle(
  642. MyAppCaptions.class.getName(),
  643. Locale.forLanguageTag("fi")); // Finnish
  644. // Localize the editor button captions
  645. grid.setEditorSaveCaption(
  646. bundle.getString(MyAppCaptions.SaveKey));
  647. grid.setEditorCancelCaption(
  648. bundle.getString(MyAppCaptions.CancelKey));
  649. ----
  650. endif::web[]
  651. [[components.grid.editing.fieldgroup]]
  652. === Binding to Data with a Binder
  653. Data binding to the item under editing is handled with a
  654. [classname]#Binder#, which you need to set with
  655. [methodname]#setEditorFieldGroup#. This is mostly useful when using
  656. special-purpose, such as to enable bean validation.
  657. For example, assuming that we want to enable bean validation for a bean such as
  658. the following:
  659. [source, java]
  660. ----
  661. public class Person implements Serializable {
  662. @NotNull
  663. @Size(min=2, max=10)
  664. private String name;
  665. @Min(1)
  666. @Max(130)
  667. private int age;
  668. ...]
  669. ----
  670. We can now use a [classname]#BeanBinder# in the [classname]#Grid# as
  671. follows:
  672. [source, java]
  673. ----
  674. Grid<Person> grid = new Grid(examplePersonList());
  675. Column<String> nameCol = grid.addColumn(Person::getName);
  676. Column<Integer> ageCol = grid.addColumn(Person::getAge);
  677. grid.setEditorEnabled(true);
  678. TextField nameEditor = new TextField();
  679. nameCol.setEditorField(nameEditor);
  680. // Enable bean validation for the data
  681. BeanBinder<Person> binder = new BeanBinder<>(Person.class);
  682. // Have some extra validation in a field
  683. binder.addField(nameEditor, "name")
  684. .withValidator(new RegexpValidator(
  685. "^\\p{Alpha}+ \\p{Alpha}+$",
  686. "Need first and last name"));
  687. grid.setEditorBinder(binder);
  688. ----
  689. To use bean validation as in the example above, you need to include an
  690. implementation of the Bean Validation API in the classpath, as described in
  691. <<dummy/../../../framework/datamodel/datamodel-forms.asciidoc#datamodel.forms.beans,"Binding Beans to Forms">>.
  692. ifdef::web[]
  693. [[components.grid.editing.validation]]
  694. === Handling Validation Errors
  695. The input fields are validated when the value is updated. The default
  696. error handler displays error indicators in the invalid fields, as well as the
  697. first error in the editor.
  698. [[figure.components.grid.errors]]
  699. .Editing a Grid Row
  700. image::img/grid-editor-errors.png[width=50%, scaledwidth=75%]
  701. You can modify the error handling by implementing a custom
  702. [interfacename]#EditorErrorHandler# or by extending the
  703. [classname]#DefaultEditorErrorHandler#.
  704. endif::web[]
  705. [[components.grid.scrolling]]
  706. == Programmatic Scrolling
  707. You can scroll to first item with [methodname]#scrollToStart()#, to end with
  708. [methodname]#scrollToEnd()#, or to a specific row with [methodname]#scrollTo()#.
  709. [[components.grid.stylegeneration]]
  710. == Generating Row or Cell Styles
  711. You can style entire rows or individual cells with a
  712. [interfacename]#StyleGenerator#, typically used through Java lambdas.
  713. [[components.grid.stylegeneration.row]]
  714. === Generating Row Styles
  715. You set a [interfacename]#StyleGenerator# to a grid with
  716. [methodname]#setStyleGenerator()#. The [methodname]#getStyle()# method gets a
  717. date item, and should return a style name or [parameter]#null# if
  718. no style is generated.
  719. For example, to add a style names to rows having certain values in one
  720. property of an item, you can style them as follows:
  721. [source, java]
  722. ----
  723. grid.setStyleGenerator(person -> {
  724. // Style based on alive status
  725. person.isAlive() ? null : "dead";
  726. });
  727. ----
  728. You could then style the rows with CSS as follows:
  729. [source, css]
  730. ----
  731. .v-grid-row.dead {
  732. color: gray;
  733. }
  734. ----
  735. [[components.grid.stylegeneration.cell]]
  736. === Generating Cell Styles
  737. You set a [interfacename]#StyleGenerator# to a grid with
  738. [methodname]#setStyleGenerator()#. The [methodname]#getStyle()# method gets
  739. a [classname]#CellReference#, which contains various information about the cell
  740. and a reference to the grid, and should return a style name or [parameter]#null#
  741. if no style is generated.
  742. For example, to add a style name to a specific column, you can match on
  743. the column as follows:
  744. [source, java]
  745. ----
  746. // Static style based on column
  747. bornColumn.setStyleGenerator(person -> "rightalign");
  748. ----
  749. You could then style the cells with a CSS rule as follows:
  750. [source, css]
  751. ----
  752. .v-grid-cell.rightalign {
  753. text-align: right;
  754. }
  755. ----
  756. [[components.grid.css]]
  757. == Styling with CSS
  758. [source, css]
  759. ----
  760. .v-grid {
  761. .v-grid-scroller, .v-grid-scroller-horizontal { }
  762. .v-grid-tablewrapper {
  763. .v-grid-header {
  764. .v-grid-row {
  765. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  766. }
  767. }
  768. .v-grid-body {
  769. .v-grid-row,
  770. .v-grid-row-stripe,
  771. .v-grid-row-has-data {
  772. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  773. }
  774. }
  775. .v-grid-footer {
  776. .v-grid-row {
  777. .v-grid-cell, .frozen, .v-grid-cell-focused { }
  778. }
  779. }
  780. }
  781. .v-grid-header-deco { }
  782. .v-grid-footer-deco { }
  783. .v-grid-horizontal-scrollbar-deco { }
  784. .v-grid-editor {
  785. .v-grid-editor-cells { }
  786. .v-grid-editor-footer {
  787. .v-grid-editor-message { }
  788. .v-grid-editor-buttons {
  789. .v-grid-editor-save { }
  790. .v-grid-editor-cancel { }
  791. }
  792. }
  793. }
  794. }
  795. ----
  796. A [classname]#Grid# has an overall [literal]#++v-grid++# style. The actual grid
  797. has three parts: a header, a body, and a footer. The scrollbar is a custom
  798. element with [literal]#++v-grid-scroller++# style. In addition, there are some
  799. decoration elements.
  800. Grid cells, whether thay are in the header, body, or footer, have a basic
  801. [literal]#++v-grid-cell++# style. Cells in a frozen column additionally have a
  802. [literal]#++frozen++# style. Rows have [literal]#++v-grid-row++# style, and
  803. every other row has additionally a [literal]#++v-grid-row-stripe++# style.
  804. The focused row has additionally [literal]#++v-grid-row-focused++# style and
  805. focused cell [literal]#++v-grid-cell-focused++#. By default, cell focus is
  806. visible, with the border stylable with [parameter]#$v-grid-cell-focused-border#
  807. parameter in Sass. Row focus has no visible styling, but can be made visible
  808. with the [parameter]#$v-grid-row-focused-background-color# parameter or with a
  809. custom style rule.
  810. In editing mode, a [literal]#++v-grid-editor++# overlay is placed on the row
  811. under editing. In addition to the editor field cells, it has an error message
  812. element, as well as the buttons.
  813. ((()))