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-table.asciidoc 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. ---
  2. title: Table
  3. order: 21
  4. layout: page
  5. ---
  6. [[components.table]]
  7. = [classname]#Table#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/grids-and-trees/table"]
  11. endif::web[]
  12. ((("[classname]#Table#", id="term.components.table", range="startofrange")))
  13. The [classname]#Table# component is intended for presenting tabular data
  14. organized in rows and columns. The [classname]#Table# is one of the most
  15. versatile components in Vaadin. Table cells can include text or arbitrary UI
  16. components. You can easily implement editing of the table data, for example
  17. clicking on a cell could change it to a text field for editing.
  18. The data contained in a [classname]#Table# is managed using the Vaadin data model (see <<dummy/../../../framework/datamodel/datamodel-overview.asciidoc#datamodel.overview,"Binding Components to Data">>), through the [classname]#Container# interface of the [classname]#Table#.
  19. This makes it possible to bind a table directly to a data source, such as a database query.
  20. Only the visible part of the table is loaded
  21. into the browser and moving the visible window with the scrollbar loads content
  22. from the server. While the data is being loaded, a tooltip will be displayed
  23. that shows the current range and total number of items in the table. The rows of
  24. the table are __items__ in the container and the columns are __properties__.
  25. Each table row (item) is identified with an __item identifier__ (IID), and each
  26. column (property) with a __property identifier__ (PID).
  27. When creating a table, you first need to define columns with
  28. [methodname]#addContainerProperty()#. This method comes in two flavors. The
  29. simpler one takes the property ID of the column and uses it also as the caption
  30. of the column. The more complex one allows differing PID and header for the
  31. column. This may make, for example, internationalization of table headers
  32. easier, because if a PID is internationalized, the internationalization has to
  33. be used everywhere where the PID is used. The complex form of the method also
  34. allows defining an icon for the column from a resource. The "default value"
  35. parameter is used when new properties (columns) are added to the table, to fill
  36. in the missing values. (This default has no meaning in the usual case, such as
  37. below, where we add items after defining the properties.)
  38. [source, java]
  39. ----
  40. Table table = new Table("The Brightest Stars");
  41. // Define two columns for the built-in container
  42. table.addContainerProperty("Name", String.class, null);
  43. table.addContainerProperty("Mag", Float.class, null);
  44. // Add a row the hard way
  45. Object newItemId = table.addItem();
  46. Item row1 = table.getItem(newItemId);
  47. row1.getItemProperty("Name").setValue("Sirius");
  48. row1.getItemProperty("Mag").setValue(-1.46f);
  49. // Add a few other rows using shorthand addItem()
  50. table.addItem(new Object[]{"Canopus", -0.72f}, 2);
  51. table.addItem(new Object[]{"Arcturus", -0.04f}, 3);
  52. table.addItem(new Object[]{"Alpha Centauri", -0.01f}, 4);
  53. // Show exactly the currently contained rows (items)
  54. table.setPageLength(table.size());
  55. ----
  56. In this example, we used an increasing [classname]#Integer# object as the Item
  57. Identifier, given as the second parameter to [methodname]#addItem()#. The actual
  58. rows are given simply as object arrays, in the same order in which the
  59. properties were added. The objects must be of the correct class, as defined in
  60. the [methodname]#addContainerProperty()# calls.
  61. .Basic Table Example
  62. image::img/table-example1.png[width=35%, scaledwidth=50%]
  63. Scalability of the [classname]#Table# is largely dictated by the container. The
  64. default [classname]#IndexedContainer# is relatively heavy and can cause
  65. scalability problems, for example, when updating the values. Use of an optimized
  66. application-specific container is recommended. Table does not have a limit for
  67. the number of items and is just as fast with hundreds of thousands of items as
  68. with just a few. With the current implementation of scrolling, there is a limit
  69. of around 500 000 rows, depending on the browser and the pixel height of rows.
  70. Common selection component features are described in
  71. <<dummy/../../../framework/components/components-selection#components.selection,"Selection Components">>.
  72. [[components.table.selecting]]
  73. == Selecting Items in a Table
  74. The [classname]#Table# allows selecting one or more items by clicking them with
  75. the mouse. When the user selects an item, the IID of the item will be set as the
  76. property of the table and a [classname]#ValueChangeEvent# is triggered. To
  77. enable selection, you need to set the table __selectable__. You will also need
  78. to set it as __immediate__ in most cases, as we do below, because without it,
  79. the change in the property will not be communicated immediately to the server.
  80. The following example shows how to enable the selection of items in a
  81. [classname]#Table# and how to handle [classname]#ValueChangeEvent# events that
  82. are caused by changes in selection. You need to handle the event with the
  83. [methodname]#valueChange()# method of the
  84. [classname]#Property.ValueChangeListener# interface.
  85. [source, java]
  86. ----
  87. // Allow selecting items from the table.
  88. table.setSelectable(true);
  89. // Send changes in selection immediately to server.
  90. table.setImmediate(true);
  91. // Shows feedback from selection.
  92. final Label current = new Label("Selected: -");
  93. // Handle selection change.
  94. table.addValueChangeListener(new Property.ValueChangeListener() {
  95. public void valueChange(ValueChangeEvent event) {
  96. current.setValue("Selected: " + table.getValue());
  97. }
  98. });
  99. ----
  100. .Table selection example
  101. image::img/table-example2.png[width=35%, scaledwidth=80%]
  102. If the user clicks on an already selected item, the selection will deselected
  103. and the table property will have [parameter]#null# value. You can disable this
  104. behaviour by setting [methodname]#setNullSelectionAllowed(false)# for the table.
  105. The selection is the value of the table's property, so you can get it with
  106. [methodname]#getValue()#. You can get it also from a reference to the table
  107. itself. In single selection mode, the value is the item identifier of the
  108. selected item or [parameter]#null# if no item is selected. In multiple selection
  109. mode (see below), the value is a [classname]#Set# of item identifiers. Notice
  110. that the set is unmodifiable, so you can not simply change it to change the
  111. selection.
  112. === Multiple Selection Mode
  113. A table can also be in __multiselect__ mode, where a user can select multiple
  114. items by clicking them with left mouse button while holding the kbd:[Ctrl] key (or kbd:[Meta] key) pressed. If kbd:[Ctrl] is not held, clicking an item will select it and
  115. other selected items are deselected. The user can select a range by selecting an
  116. item, holding the kbd:[Shift] key pressed, and clicking another item, in which case
  117. all the items between the two are also selected. Multiple ranges can be selected
  118. by first selecting a range, then selecting an item while holding kbd:[Ctrl], and then
  119. selecting another item with both kbd:[Ctrl] and kbd:[Shift] pressed.
  120. The multiselect mode is enabled with the [methodname]#setMultiSelect()# method
  121. of the [classname]#AbstractSelect# superclass of [classname]#Table#. Setting
  122. table in multiselect mode does not implicitly set it as __selectable__, so it
  123. must be set separately.
  124. The [methodname]#setMultiSelectMode()# property affects the control of multiple
  125. selection: [parameter]#MultiSelectMode.DEFAULT# is the default behaviour, which
  126. requires holding the kbd:[Ctrl] (or kbd:[Meta]) key pressed while selecting items, while in
  127. [parameter]#MultiSelectMode.SIMPLE# holding the kbd:[Ctrl] key is not needed. In the
  128. simple mode, items can only be deselected by clicking them.
  129. [[components.table.features]]
  130. == Table Features
  131. === Page Length and Scrollbar
  132. The default style for [classname]#Table# provides a table with a scrollbar. The
  133. scrollbar is located at the right side of the table and becomes visible when the
  134. number of items in the table exceeds the page length, that is, the number of
  135. visible items. You can set the page length with [methodname]#setPageLength()#.
  136. Setting the page length to zero makes all the rows in a table visible, no matter
  137. how many rows there are. Notice that this also effectively disables buffering,
  138. as all the entire table is loaded to the browser at once. Using such tables to
  139. generate reports does not scale up very well, as there is some inevitable
  140. overhead in rendering a table with Ajax. For very large reports, generating HTML
  141. directly is a more scalable solution.
  142. [[components.table.features.resizing]]
  143. === Resizing Columns
  144. You can set the width of a column programmatically from the server-side with
  145. [methodname]#setColumnWidth()#. The column is identified by the property ID and
  146. the width is given in pixels.
  147. The user can resize table columns by dragging the resize handle between two
  148. columns. Resizing a table column causes a [classname]#ColumnResizeEvent#, which
  149. you can handle with a [classname]#Table.ColumnResizeListener#. The table must be
  150. set in immediate mode if you want to receive the resize events immediately,
  151. which is typical.
  152. [source, java]
  153. ----
  154. table.addColumnResizeListener(new Table.ColumnResizeListener(){
  155. public void columnResize(ColumnResizeEvent event) {
  156. // Get the new width of the resized column
  157. int width = event.getCurrentWidth();
  158. // Get the property ID of the resized column
  159. String column = (String) event.getPropertyId();
  160. // Do something with the information
  161. table.setColumnFooter(column, String.valueOf(width) + "px");
  162. }
  163. });
  164. // Must be immediate to send the resize events immediately
  165. table.setImmediate(true);
  166. ----
  167. See <<figure.component.table.columnresize>> for a result after the columns of a
  168. table has been resized.
  169. [[figure.component.table.columnresize]]
  170. .Resizing Columns
  171. image::img/table-column-resize.png[width=50%, scaledwidth=80%]
  172. [[components.table.features.reordering]]
  173. === Reordering Columns
  174. If [methodname]#setColumnReorderingAllowed(true)# is set, the user can reorder
  175. table columns by dragging them with the mouse from the column header,
  176. [[components.table.features.collapsing]]
  177. === Collapsing Columns
  178. When [methodname]#setColumnCollapsingAllowed(true)# is set, the right side of
  179. the table header shows a drop-down list that allows selecting which columns are
  180. shown. Collapsing columns is different than hiding columns with
  181. [methodname]#setVisibleColumns()#, which hides the columns completely so that
  182. they can not be made visible (uncollapsed) from the user interface.
  183. You can collapse columns programmatically with
  184. [methodname]#setColumnCollapsed()#. Collapsing must be enabled before collapsing
  185. columns with the method or it will throw an [classname]#IllegalAccessException#.
  186. [source, java]
  187. ----
  188. // Allow the user to collapse and uncollapse columns
  189. table.setColumnCollapsingAllowed(true);
  190. // Collapse this column programmatically
  191. try {
  192. table.setColumnCollapsed("born", true);
  193. } catch (IllegalAccessException e) {
  194. // Can't occur - collapsing was allowed above
  195. System.err.println("Something horrible occurred");
  196. }
  197. // Give enough width for the table to accommodate the
  198. // initially collapsed column later
  199. table.setWidth("250px");
  200. ----
  201. See <<figure.component.table.columncollapsing>>.
  202. [[figure.component.table.columncollapsing]]
  203. .Collapsing Columns
  204. image::img/table-column-collapsing.png[width=40%, scaledwidth=80%]
  205. If the table has undefined width, it minimizes its width to fit the width of the
  206. visible columns. If some columns are initially collapsed, the width of the table
  207. may not be enough to accomodate them later, which will result in an ugly
  208. horizontal scrollbar. You should consider giving the table enough width to
  209. accomodate columns uncollapsed by the user.
  210. [[components.table.features.components]]
  211. === Components Inside a Table
  212. The cells of a [classname]#Table# can contain any user interface components, not
  213. just strings. If the rows are higher than the row height defined in the default
  214. theme, you have to define the proper row height in a custom theme.
  215. When handling events for components inside a [classname]#Table#, such as for the
  216. [classname]#Button# in the example below, you usually need to know the item the
  217. component belongs to. Components do not themselves know about the table or the
  218. specific item in which a component is contained. Therefore, the handling method
  219. must use some other means for finding out the Item ID of the item. There are a
  220. few possibilities. Usually the easiest way is to use the [methodname]#setData()#
  221. method to attach an arbitrary object to a component. You can subclass the
  222. component and include the identity information there. You can also simply search
  223. the entire table for the item with the component, although that solution may not
  224. be so scalable.
  225. The example below includes table rows with a [classname]#Label# in HTML content
  226. mode, a multiline [classname]#TextField#, a [classname]#CheckBox#, and a
  227. [classname]#Button# that shows as a link.
  228. [source, java]
  229. ----
  230. // Create a table and add a style to
  231. // allow setting the row height in theme.
  232. Table table = new Table();
  233. table.addStyleName("components-inside");
  234. /* Define the names and data types of columns.
  235. * The "default value" parameter is meaningless here. */
  236. table.addContainerProperty("Sum", Label.class, null);
  237. table.addContainerProperty("Is Transferred", CheckBox.class, null);
  238. table.addContainerProperty("Comments", TextField.class, null);
  239. table.addContainerProperty("Details", Button.class, null);
  240. /* Add a few items in the table. */
  241. for (int i=0; i<100; i++) {
  242. // Create the fields for the current table row
  243. Label sumField = new Label(String.format(
  244. "Sum is <b>$%04.2f</b><br/><i>(VAT incl.)</i>",
  245. new Object[] {new Double(Math.random()*1000)}),
  246. ContentMode.HTML);
  247. CheckBox transferredField = new CheckBox("is transferred");
  248. // Multiline text field. This required modifying the
  249. // height of the table row.
  250. TextField commentsField = new TextField();
  251. commentsField.setRows(3);
  252. // The Table item identifier for the row.
  253. Integer itemId = new Integer(i);
  254. // Create a button and handle its click. A Button does not
  255. // know the item it is contained in, so we have to store the
  256. // item ID as user-defined data.
  257. Button detailsField = new Button("show details");
  258. detailsField.setData(itemId);
  259. detailsField.addClickListener(new Button.ClickListener() {
  260. public void buttonClick(ClickEvent event) {
  261. // Get the item identifier from the user-defined data.
  262. Integer iid = (Integer)event.getButton().getData();
  263. Notification.show("Link " +
  264. iid.intValue() + " clicked.");
  265. }
  266. });
  267. detailsField.addStyleName("link");
  268. // Create the table row.
  269. table.addItem(new Object[] {sumField, transferredField,
  270. commentsField, detailsField},
  271. itemId);
  272. }
  273. // Show just three rows because they are so high.
  274. table.setPageLength(3);
  275. ----
  276. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.table.components.components2[on-line example, window="_blank"].
  277. The row height has to be set higher than the default with a style rule such as
  278. the following:
  279. [source, css]
  280. ----
  281. /* Table rows contain three-row TextField components. */
  282. .v-table-components-inside .v-table-cell-content {
  283. height: 54px;
  284. }
  285. ----
  286. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.table.components.components2[on-line example, window="_blank"].
  287. The table will look as shown in <<figure.components.table.components-inside>>.
  288. [[figure.components.table.components-inside]]
  289. .Components in a Table
  290. image::img/table-components.png[width=70%, scaledwidth=100%]
  291. [[components.table.features.iterating]]
  292. === Iterating Over a Table
  293. As the items in a [classname]#Table# are not indexed, iterating over the items
  294. has to be done using an iterator. The [methodname]#getItemIds()# method of the
  295. [classname]#Container# interface of [classname]#Table# returns a
  296. [classname]#Collection# of item identifiers over which you can iterate using an
  297. [classname]#Iterator#. For an example about iterating over a [classname]#Table#,
  298. please see
  299. <<dummy/../../../framework/datamodel/datamodel-container#datamodel.container,"Collecting
  300. Items in Containers">>. Notice that you may not modify the [classname]#Table#
  301. during iteration, that is, add or remove items. Changing the data is allowed.
  302. [[components.table.features.filtering]]
  303. === Filtering Table Contents
  304. A table can be filtered if its container data source implements the
  305. [classname]#Filterable# interface, as the default [classname]#IndexedContainer#
  306. does. See
  307. <<dummy/../../../framework/datamodel/datamodel-container#datamodel.container.filtered,"Filterable
  308. Containers">>. ((("Container",
  309. "Filterable")))
  310. [[components.table.editing]]
  311. == Editing the Values in a Table
  312. Normally, a [classname]#Table# simply displays the items and their fields as
  313. text. If you want to allow the user to edit the values, you can either put them
  314. inside components as we did earlier or simply call
  315. [methodname]#setEditable(true)#, in which case the cells are automatically
  316. turned into editable fields.
  317. Let us begin with a regular table with a some columns with usual Java types,
  318. namely a [classname]#Date#, [classname]#Boolean#, and a [classname]#String#.
  319. [source, java]
  320. ----
  321. // Create a table. It is by default not editable.
  322. Table table = new Table();
  323. // Define the names and data types of columns.
  324. table.addContainerProperty("Date", Date.class, null);
  325. table.addContainerProperty("Work", Boolean.class, null);
  326. table.addContainerProperty("Comments", String.class, null);
  327. ...
  328. ----
  329. You could put the table in editable mode right away. We continue the example by
  330. adding a check box to switch the table between normal and editable modes:
  331. [source, java]
  332. ----
  333. CheckBox editable = new CheckBox("Editable", true);
  334. editable.addValueChangeListener(valueChange -> // Java 8
  335. table.setEditable((Boolean) editable.getValue()));
  336. ----
  337. Now, when you check to checkbox, the components in the table turn into editable
  338. fields, as shown in <<figure.component.table.editable>>.
  339. [[figure.component.table.editable]]
  340. .A Table in Normal and Editable Mode
  341. image::img/table-editable3.png[width=100%, scaledwidth=100%]
  342. [[components.table.editing.fieldfactories]]
  343. === Field Factories
  344. The field components that allow editing the values of particular types in a
  345. table are defined in a field factory that implements the
  346. [classname]#TableFieldFactory# interface. The default implementation is
  347. [classname]#DefaultFieldFactory#, which offers the following crude mappings:
  348. .Type to Field Mappings in [classname]#DefaultFieldFactory#
  349. [options="header",cols="2,5"]
  350. |===============
  351. |Property Type|Mapped to Field Class
  352. |[classname]#Date#|A [classname]#DateField#.
  353. |[classname]#Boolean#|A [classname]#CheckBox#.
  354. |[classname]#Item#|A [classname]#Form# (deprecated in Vaadin 7). The fields of the form are automatically created from the item's properties using a [classname]#FormFieldFactory#. The normal use for this property type is inside a [classname]#Form# and is less useful inside a [classname]#Table#.
  355. |__other__|A [classname]#TextField#. The text field manages conversions from the basic types, if possible.
  356. |===============
  357. Field factories are covered with more detail in
  358. <<dummy/../../../framework/datamodel/datamodel-itembinding#datamodel.itembinding,"Creating
  359. Forms by Binding Fields to Items">>. You could just implement the
  360. [classname]#TableFieldFactory# interface, but we recommend that you extend the
  361. [classname]#DefaultFieldFactory# according to your needs. In the default
  362. implementation, the mappings are defined in the
  363. [methodname]#createFieldByPropertyType()# method (you might want to look at the
  364. source code) both for tables and forms.
  365. ifdef::web[]
  366. [[components.table.editing.navigation]]
  367. === Navigation in Editable Mode
  368. In the editable mode, the editor fields can have focus. Pressing Tab moves the
  369. focus to next column or, at the last column, to the first column of the next
  370. item. Respectively, pressing kbd:[Shift+Tab] moves the focus backward. If the focus is
  371. in the last column of the last visible item, the pressing kbd:[Tab] moves the focus
  372. outside the table. Moving backward from the first column of the first item moves
  373. the focus to the table itself. Some updates to the table, such as changing the
  374. headers or footers or regenerating a column, can move the focus from an editor
  375. component to the table itself.
  376. The default behaviour may be undesirable in many cases. For example, the focus
  377. also goes through any read-only editor fields and can move out of the table
  378. inappropriately. You can provide better navigation is to use event handler for
  379. shortcut keys such as kbd:[Tab], kbd:[Arrow Up], kbd:[Arrow Down], and kbd:[Enter].
  380. [source, java]
  381. ----
  382. // Keyboard navigation
  383. class KbdHandler implements Handler {
  384. Action tab_next = new ShortcutAction("Tab",
  385. ShortcutAction.KeyCode.TAB, null);
  386. Action tab_prev = new ShortcutAction("Shift+Tab",
  387. ShortcutAction.KeyCode.TAB,
  388. new int[] {ShortcutAction.ModifierKey.SHIFT});
  389. Action cur_down = new ShortcutAction("Down",
  390. ShortcutAction.KeyCode.ARROW_DOWN, null);
  391. Action cur_up = new ShortcutAction("Up",
  392. ShortcutAction.KeyCode.ARROW_UP, null);
  393. Action enter = new ShortcutAction("Enter",
  394. ShortcutAction.KeyCode.ENTER, null);
  395. public Action[] getActions(Object target, Object sender) {
  396. return new Action[] {tab_next, tab_prev, cur_down,
  397. cur_up, enter};
  398. }
  399. public void handleAction(Action action, Object sender,
  400. Object target) {
  401. if (target instanceof TextField) {
  402. // Move according to keypress
  403. int itemid = (Integer) ((TextField) target).getData();
  404. if (action == tab_next || action == cur_down)
  405. itemid++;
  406. else if (action == tab_prev || action == cur_up)
  407. itemid--;
  408. // On enter, just stay where you were. If we did
  409. // not catch the enter action, the focus would be
  410. // moved to wrong place.
  411. if (itemid >= 0 && itemid < table.size()) {
  412. TextField newTF = valueFields.get(itemid);
  413. if (newTF != null)
  414. newTF.focus();
  415. }
  416. }
  417. }
  418. }
  419. // Panel that handles keyboard navigation
  420. Panel navigator = new Panel();
  421. navigator.addStyleName(Reindeer.PANEL_LIGHT);
  422. navigator.addComponent(table);
  423. navigator.addActionHandler(new KbdHandler());
  424. ----
  425. The main issue in implementing keyboard navigation in an editable table is that
  426. the editor fields do not know the table they are in. To find the parent table,
  427. you can either look up in the component container hierarchy or simply store a
  428. reference to the table with [methodname]#setData()# in the field component. The
  429. other issue is that you can not acquire a reference to an editor field from the
  430. [classname]#Table# component. One solution is to use some external collection,
  431. such as a [classname]#HashMap#, to map item IDs to the editor fields.
  432. [source, java]
  433. ----
  434. // Can't access the editable components from the table so
  435. // must store the information
  436. final HashMap<Integer,TextField> valueFields =
  437. new HashMap<Integer,TextField>();
  438. ----
  439. The map has to be filled in a [classname]#TableFieldFactory#, such as in the
  440. following. You also need to set the reference to the table there and you can
  441. also set the initial focus there.
  442. [source, java]
  443. ----
  444. table.setTableFieldFactory(new TableFieldFactory () {
  445. public Field createField(Container container, Object itemId,
  446. Object propertyId, Component uiContext) {
  447. TextField field = new TextField((String) propertyId);
  448. // User can only edit the numeric column
  449. if ("Source of Fear".equals(propertyId))
  450. field.setReadOnly(true);
  451. else { // The numeric column
  452. // The field needs to know the item it is in
  453. field.setData(itemId);
  454. // Remember the field
  455. valueFields.put((Integer) itemId, field);
  456. // Focus the first editable value
  457. if (((Integer)itemId) == 0)
  458. field.focus();
  459. }
  460. return field;
  461. }
  462. });
  463. ----
  464. The issues are complicated by the fact that the editor fields are not generated
  465. for the entire table, but only for a cache window that includes the visible
  466. items and some items above and below it. For example, if the beginning of a big
  467. scrollable table is visible, the editor component for the last item does not
  468. exist. This issue is relevant mostly if you want to have wrap-around navigation
  469. that jumps from the last to first item and vice versa.
  470. endif::web[]
  471. [[components.table.headersfooters]]
  472. == Column Headers and Footers
  473. [classname]#Table# supports both column headers and footers; the headers are
  474. enabled by default.
  475. [[components.table.headersfooters.headers]]
  476. === Headers
  477. The table header displays the column headers at the top of the table. You can
  478. use the column headers to reorder or resize the columns, as described earlier.
  479. By default, the header of a column is the property ID of the column, unless
  480. given explicitly with [methodname]#setColumnHeader()#.
  481. [source, java]
  482. ----
  483. // Define the properties
  484. table.addContainerProperty("lastname", String.class, null);
  485. table.addContainerProperty("born", Integer.class, null);
  486. table.addContainerProperty("died", Integer.class, null);
  487. // Set nicer header names
  488. table.setColumnHeader("lastname", "Name");
  489. table.setColumnHeader("born", "Born");
  490. table.setColumnHeader("died", "Died");
  491. ----
  492. The text of the column headers and the visibility of the header depends on the
  493. __column header mode__. The header is visible by default, but you can disable it
  494. with [methodname]#setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN)#.
  495. [[components.table.headersfooters.footers]]
  496. === Footers
  497. The table footer can be useful for displaying sums or averages of values in a
  498. column, and so on. The footer is not visible by default; you can enable it with
  499. [methodname]#setFooterVisible(true)#. Unlike in the header, the column headers
  500. are empty by default. You can set their value with
  501. [methodname]#setColumnFooter()#. The columns are identified by their property
  502. ID.
  503. The following example shows how to calculate average of the values in a column:
  504. [source, java]
  505. ----
  506. // Have a table with a numeric column
  507. Table table = new Table("Custom Table Footer");
  508. table.addContainerProperty("Name", String.class, null);
  509. table.addContainerProperty("Died At Age", Integer.class, null);
  510. // Insert some data
  511. Object people[][] = { {"Galileo", 77},
  512. {"Monnier", 83},
  513. {"Vaisala", 79},
  514. {"Oterma", 86}};
  515. for (int i=0; i<people.length; i++)
  516. table.addItem(people[i], new Integer(i));
  517. // Calculate the average of the numeric column
  518. double avgAge = 0;
  519. for (int i=0; i<people.length; i++)
  520. avgAge += (Integer) people[i][1];
  521. avgAge /= people.length;
  522. // Set the footers
  523. table.setFooterVisible(true);
  524. table.setColumnFooter("Name", "Average");
  525. table.setColumnFooter("Died At Age", String.valueOf(avgAge));
  526. // Adjust the table height a bit
  527. table.setPageLength(table.size());
  528. ----
  529. The resulting table is shown in
  530. <<figure.components.table.headersfooters.footer>>.
  531. [[figure.components.table.headersfooters.footer]]
  532. .A Table with a Footer
  533. image::img/table-footer.png[width=25%, scaledwidth=40%]
  534. [[components.table.headersfooters.clicks]]
  535. === Handling Mouse Clicks on Headers and Footers
  536. Normally, when the user clicks a column header, the table will be sorted by the
  537. column, assuming that the data source is [classname]#Sortable# and sorting is
  538. not disabled. In some cases, you might want some other functionality when the
  539. user clicks the column header, such as selecting the column in some way.
  540. Clicks in the header cause a [classname]#HeaderClickEvent#, which you can handle
  541. with a [classname]#Table.HeaderClickListener#. Click events on the table header
  542. (and footer) are, like button clicks, sent immediately to server, so there is no
  543. need to set [methodname]#setImmediate()#.
  544. [source, java]
  545. ----
  546. // Handle the header clicks
  547. table.addHeaderClickListener(new Table.HeaderClickListener() {
  548. public void headerClick(HeaderClickEvent event) {
  549. String column = (String) event.getPropertyId();
  550. Notification.show("Clicked " + column +
  551. "with " + event.getButtonName());
  552. }
  553. });
  554. // Disable the default sorting behavior
  555. table.setSortDisabled(true);
  556. ----
  557. Setting a click handler does not automatically disable the sorting behavior of
  558. the header; you need to disable it explicitly with
  559. [methodname]#setSortDisabled(true)#. Header click events are not sent when the
  560. user clicks the column resize handlers to drag them.
  561. The [classname]#HeaderClickEvent# object provides the identity of the clicked
  562. column with [methodname]#getPropertyId()#. The [methodname]#getButton()# reports
  563. the mouse button with which the click was made: [parameter]#BUTTON_LEFT#,
  564. [parameter]#BUTTON_RIGHT#, or [parameter]#BUTTON_MIDDLE#. The
  565. [methodname]#getButtonName()# a human-readable button name in English: "
  566. [parameter]#left#", " [parameter]#right#", or " [parameter]#middle#". The
  567. [methodname]#isShiftKey()#, [methodname]#isCtrlKey()#, etc., methods indicate if
  568. the kbd:[Shift], kbd:[Ctrl], kbd:[Alt] or other modifier keys were pressed during the click.
  569. Clicks in the footer cause a [classname]#FooterClickEvent#, which you can handle
  570. with a [classname]#Table.FooterClickListener#. Footers do not have any default
  571. click behavior, like the sorting in the header. Otherwise, handling clicks in
  572. the footer is equivalent to handling clicks in the header.
  573. [[components.table.columngenerator]]
  574. == Generated Table Columns
  575. A table can have generated columns which values can be calculated based on the
  576. values in other columns. The columns are generated with a class implementing the
  577. [interfacename]#Table.ColumnGenerator# interface.
  578. The [classname]#GeneratedPropertyContainer# described in
  579. <<dummy/../../../framework/datamodel/datamodel-container#datamodel.container.gpc,"GeneratedPropertyContainer">>
  580. is another way to accomplish the same task at container level. In addition to
  581. generating values, you can also use the feature for formatting or styling
  582. columns.
  583. ifdef::web[]
  584. [[components.table.columngenerator.generator]]
  585. === Defining a Column Generator
  586. Column generators are objects that implement the
  587. [classname]#Table.ColumnGenerator# interface and its
  588. [methodname]#generateCell()# method. The method gets the identity of the item
  589. and column as its parameters, in addition to the table object, and has to return
  590. a component. The interface is functional, so you can also define it by a lambda
  591. expression or a method reference in Java 8.
  592. The following example defines a generator for formatting [classname]#Double#
  593. valued fields according to a format string (as in
  594. [classname]#java.util.Formatter#).
  595. [source, java]
  596. ----
  597. /** Formats the value in a column containing Double objects. */
  598. class ValueColumnGenerator implements Table.ColumnGenerator {
  599. String format; /* Format string for the Double values. */
  600. /**
  601. * Creates double value column formatter with the given
  602. * format string.
  603. */
  604. public ValueColumnGenerator(String format) {
  605. this.format = format;
  606. }
  607. /**
  608. * Generates the cell containing the Double value.
  609. * The column is irrelevant in this use case.
  610. */
  611. public Component generateCell(Table source, Object itemId,
  612. Object columnId) {
  613. // Get the object stored in the cell as a property
  614. Property prop =
  615. source.getItem(itemId).getItemProperty(columnId);
  616. if (prop.getType().equals(Double.class)) {
  617. Label label = new Label(String.format(format,
  618. new Object[] { (Double) prop.getValue() }));
  619. // Set styles for the column: one indicating that it's
  620. // a value and a more specific one with the column
  621. // name in it. This assumes that the column name
  622. // is proper for CSS.
  623. label.addStyleName("column-type-value");
  624. label.addStyleName("column-" + (String) columnId);
  625. return label;
  626. }
  627. return null;
  628. }
  629. }
  630. ----
  631. The column generator is called for all the visible (or more accurately cached)
  632. items in a table. If the user scrolls the table to another position in the
  633. table, the columns of the new visible rows are generated dynamically.
  634. Generated column cells are automatically updated when a property value in the
  635. table row changes. Note that a generated cell, even if it is a field, does not
  636. normally have a property value bound to the table's container, so changes in
  637. generated columns do not trigger updates in other generated columns. It should
  638. also be noted that if a generated column cell depends on values in other rows,
  639. changes in the other rows do not trigger automatic update. You can get notified
  640. of such value changes by listening for them with a
  641. [interfacename]#ValueChangeListener# in the generated components. If you do so,
  642. you must remove such listeners when the generated components are detached from
  643. the UI or otherwise the listeners will accumulate in the container when the
  644. table is scrolled back and forth, causing possibly severe memory leak.
  645. endif::web[]
  646. ifdef::web[]
  647. [[components.table.columngenerator.adding]]
  648. === Adding Generated Columns
  649. You add new generated columns to a [classname]#Table# with
  650. [methodname]#addGeneratedColumn()#. It takes a property ID of the generated
  651. column as the first parameter and the generator as the second.
  652. [source, java]
  653. ----
  654. // Define the generated columns and their generators
  655. table.addGeneratedColumn("date", // Java 8:
  656. this::generateNonEditableCell);
  657. table.addGeneratedColumn("price",
  658. new PriceColumnGenerator());
  659. table.addGeneratedColumn("consumption",
  660. new ConsumptionColumnGenerator());
  661. table.addGeneratedColumn("dailycost",
  662. new DailyCostColumnGenerator());
  663. ----
  664. Notice that the [methodname]#addGeneratedColumn()# always places the generated
  665. columns as the last column, even if you defined some other order previously. You
  666. will have to set the proper order with [methodname]#setVisibleColumns()#.
  667. [source, java]
  668. ----
  669. table.setVisibleColumns("date", "quantity", "price", "total");
  670. ----
  671. endif::web[]
  672. ifdef::web[]
  673. [[components.table.columngenerator.editable]]
  674. === Generators in Editable Table
  675. When you set a table as [parameter]#editable#, table cells change to editable
  676. fields. When the user changes the values in the fields, the generated cells in
  677. the same row are updated automatically. However, putting a table with generated
  678. columns in editable mode has a few quirks. One is that the editable mode does
  679. not affect generated columns. You have two alternatives: either you generate the
  680. editing fields in the generator or, in case of formatter generators, remove the
  681. generators in the editable mode to allow editing the values. The following
  682. example uses the latter approach.
  683. [source, java]
  684. ----
  685. // Have a check box that allows the user
  686. // to make the quantity and total columns editable.
  687. final CheckBox editable = new CheckBox(
  688. "Edit the input values - calculated columns are regenerated");
  689. editable.setImmediate(true);
  690. editable.addClickListener(new ClickListener() {
  691. public void buttonClick(ClickEvent event) {
  692. table.setEditable(editable.booleanValue());
  693. // The columns may not be generated when we want to
  694. // have them editable.
  695. if (editable.booleanValue()) {
  696. table.removeGeneratedColumn("quantity");
  697. table.removeGeneratedColumn("total");
  698. } else { // Not editable
  699. // Show the formatted values.
  700. table.addGeneratedColumn("quantity",
  701. new ValueColumnGenerator("%.2f l"));
  702. table.addGeneratedColumn("total",
  703. new ValueColumnGenerator("%.2f e"));
  704. }
  705. // The visible columns are affected by removal
  706. // and addition of generated columns so we have
  707. // to redefine them.
  708. table.setVisibleColumns("date", "quantity",
  709. "price", "total", "consumption", "dailycost");
  710. }
  711. });
  712. ----
  713. You will also have to set the editing fields in [parameter]#immediate# mode to
  714. have the update occur immediately when an edit field loses the focus. You can
  715. set the fields in [parameter]#immediate# mode with the a custom
  716. [classname]#TableFieldFactory#, such as the one given below, that just extends
  717. the default implementation to set the mode:
  718. [source, java]
  719. ----
  720. public class ImmediateFieldFactory extends DefaultFieldFactory {
  721. public Field createField(Container container,
  722. Object itemId,
  723. Object propertyId,
  724. Component uiContext) {
  725. // Let the DefaultFieldFactory create the fields...
  726. Field field = super.createField(container, itemId,
  727. propertyId, uiContext);
  728. // ...and just set them as immediate.
  729. ((AbstractField)field).setImmediate(true);
  730. return field;
  731. }
  732. }
  733. ...
  734. table.setTableFieldFactory(new ImmediateFieldFactory());
  735. ----
  736. If you generate the editing fields with the column generator, you avoid having
  737. to use such a field factory, but of course have to generate the fields for both
  738. normal and editable modes.
  739. <<figure.ui.table.generated>> shows a table with columns calculated (blue) and
  740. simply formatted (black) with column generators.
  741. [[figure.ui.table.generated]]
  742. .Table with generated columns
  743. image::img/table-generatedcolumns1.png[width=90%, scaledwidth=100%]
  744. endif::web[]
  745. [[components.table.columnformatting]]
  746. == Formatting Table Columns
  747. The displayed values of properties shown in a table are normally formatted using
  748. the [methodname]#toString()# method of each property. Customizing the format in
  749. a table column can be done in several ways:
  750. * Using [classname]#ColumnGenerator# to generate a second column that is formatted. The original column needs to be set invisible. See <<components.table.columngenerator>>.
  751. * Using a [classname]#Converter# to convert between the property data model and its representation in the table.
  752. * Using a [classname]#GeneratedPropertyContainer# as a wrapper around the actual container to provide formatting.
  753. * Overriding the default [methodname]#formatPropertyValue()# in [classname]#Table#.
  754. As using a [classname]#PropertyFormatter# is generally much more awkward than
  755. overriding the [methodname]#formatPropertyValue()#, its use is not described
  756. here.
  757. You can override [methodname]#formatPropertyValue()# as is done in the following
  758. example:
  759. [source, java]
  760. ----
  761. // Create a table that overrides the default
  762. // property (column) format
  763. final Table table = new Table("Formatted Table") {
  764. @Override
  765. protected String formatPropertyValue(Object rowId,
  766. Object colId, Property property) {
  767. // Format by property type
  768. if (property.getType() == Date.class) {
  769. SimpleDateFormat df =
  770. new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  771. return df.format((Date)property.getValue());
  772. }
  773. return super.formatPropertyValue(rowId, colId, property);
  774. }
  775. };
  776. // The table has some columns
  777. table.addContainerProperty("Time", Date.class, null);
  778. ... Fill the table with data ...
  779. ----
  780. You can also distinguish between columns by the [parameter]#colId# parameter,
  781. which is the property ID of the column. [classname]#DecimalFormat# is useful for
  782. formatting decimal values.
  783. [source, java]
  784. ----
  785. ... in formatPropertyValue() ...
  786. } else if ("Value".equals(pid)) {
  787. // Format a decimal value for a specific locale
  788. DecimalFormat df = new DecimalFormat("#.00",
  789. new DecimalFormatSymbols(locale));
  790. return df.format((Double) property.getValue());
  791. }
  792. ...
  793. table.addContainerProperty("Value", Double.class, null);
  794. ----
  795. A table with the formatted date and decimal value columns is shown in
  796. <<figure.components.table.columnformatting>>.
  797. [[figure.components.table.columnformatting]]
  798. .Formatted Table columns
  799. image::img/table-columnformatting.png[width=40%, scaledwidth=50%]
  800. You can use CSS for further styling of table rows, columns, and individual cells by using a [classname]#CellStyleGenerator#.
  801. ifdef::web[It is described in <<components.table.css>>.]
  802. [[components.table.css]]
  803. == CSS Style Rules
  804. [source, css]
  805. ----
  806. .v-table {}
  807. .v-table-header-wrap {}
  808. .v-table-header {}
  809. .v-table-header-cell {}
  810. .v-table-resizer {} /* Column resizer handle. */
  811. .v-table-caption-container {}
  812. .v-table-body {}
  813. .v-table-row-spacer {}
  814. .v-table-table {}
  815. .v-table-row {}
  816. .v-table-cell-content {}
  817. ----
  818. Notice that some of the widths and heights in a table are calculated dynamically
  819. and can not be set in CSS.
  820. ifdef::web[]
  821. [[components.table.css.cellstylegenerator]]
  822. === Generating Cell Styles With [interfacename]#CellStyleGenerator#
  823. The [classname]#Table.CellStyleGenerator# interface allows you to set the CSS
  824. style for each individual cell in a table. You need to implement the
  825. [methodname]#getStyle()#, which gets the row (item) and column (property)
  826. identifiers as parameters and can return a style name for the cell. The returned
  827. style name will be concatenated to prefix "
  828. [literal]#++v-table-cell-content-++#".
  829. The [methodname]#getStyle()# is called also for each row, so that the
  830. [parameter]#propertyId# parameter is [literal]#++null++#. This allows setting a
  831. row style.
  832. Alternatively, you can use a [classname]#Table.ColumnGenerator# (see
  833. <<components.table.columngenerator>>) to generate the actual UI components of
  834. the cells and add style names to them.
  835. [source, java]
  836. ----
  837. Table table = new Table("Table with Cell Styles");
  838. table.addStyleName("checkerboard");
  839. // Add some columns in the table. In this example, the property
  840. // IDs of the container are integers so we can determine the
  841. // column number easily.
  842. table.addContainerProperty("0", String.class, null, "", null, null);
  843. for (int i=0; i<8; i++)
  844. table.addContainerProperty(""+(i+1), String.class, null,
  845. String.valueOf((char) (65+i)), null, null);
  846. // Add some items in the table.
  847. table.addItem(new Object[]{
  848. "1", "R", "N", "B", "Q", "K", "B", "N", "R"}, new Integer(0));
  849. table.addItem(new Object[]{
  850. "2", "P", "P", "P", "P", "P", "P", "P", "P"}, new Integer(1));
  851. for (int i=2; i<6; i++)
  852. table.addItem(new Object[]{String.valueOf(i+1),
  853. "", "", "", "", "", "", "", ""}, new Integer(i));
  854. table.addItem(new Object[]{
  855. "7", "P", "P", "P", "P", "P", "P", "P", "P"}, new Integer(6));
  856. table.addItem(new Object[]{
  857. "8", "R", "N", "B", "Q", "K", "B", "N", "R"}, new Integer(7));
  858. table.setPageLength(8);
  859. // Set cell style generator
  860. table.setCellStyleGenerator(new Table.CellStyleGenerator() {
  861. public String getStyle(Object itemId, Object propertyId) {
  862. // Row style setting, not relevant in this example.
  863. if (propertyId == null)
  864. return "green"; // Will not actually be visible
  865. int row = ((Integer)itemId).intValue();
  866. int col = Integer.parseInt((String)propertyId);
  867. // The first column.
  868. if (col == 0)
  869. return "rowheader";
  870. // Other cells.
  871. if ((row+col)%2 == 0)
  872. return "black";
  873. else
  874. return "white";
  875. }
  876. });
  877. ----
  878. You can then style the cells, for example, as follows:
  879. [source, css]
  880. ----
  881. /* Center the text in header. */
  882. .v-table-header-cell {
  883. text-align: center;
  884. }
  885. /* Basic style for all cells. */
  886. .v-table-checkerboard .v-table-cell-content {
  887. text-align: center;
  888. vertical-align: middle;
  889. padding-top: 12px;
  890. width: 20px;
  891. height: 28px;
  892. }
  893. /* Style specifically for the row header cells. */
  894. .v-table-cell-content-rowheader {
  895. background: #E7EDF3
  896. url(../default/table/img/header-bg.png) repeat-x scroll 0 0;
  897. }
  898. /* Style specifically for the "white" cells. */
  899. .v-table-cell-content-white {
  900. background: white;
  901. color: black;
  902. }
  903. /* Style specifically for the "black" cells. */
  904. .v-table-cell-content-black {
  905. background: black;
  906. color: white;
  907. }
  908. ----
  909. The table will look as shown in <<figure.components.table.cell-style>>.
  910. [[figure.components.table.cell-style]]
  911. .Cell style generator for a Table
  912. image::img/table-cellstylegenerator1.png[width=50%, scaledwidth=80%]
  913. endif::web[]
  914. (((range="endofrange", startref="term.components.table")))