Grid
- Content
- Dynamic Height
- Selection
- Columns
- Sorting
- Filtering
- Item Details
- Context Menu
- Drag and Drop
- Inline Editing
- Styling Rows and Columns
- Theme Variants
- Related Components
Grid is a component for showing tabular data.
Content
A basic Grid uses plain text to display information in rows and columns. Rich content can be used to provide additional information in a more legible fashion. Components such as input fields and Button are also supported.
Component Renderer vs Template Renderer
As demonstrated in the example above, custom content can be rendered using component renderers or template renderers.
Component Renderer
Component renderers are easy to build but slow to render. For a given column, they generate a component for each item in the dataset. The rendered components are fully controllable on the server side.
For each rendered cell, Grid creates a corresponding component instance on the server side. A dataset of 100 items and 10 columns using component renderer adds up to 1000 components that need to be managed. The more components used in a component renderer, the more it will negatively affect performance.
Component renderers are very flexible and easy to use, but should be used with caution. They are better suited as editors since only a single row can be edited at a time. They can also be used for detail rows.
Template Renderer
Template renderers render quickly but require writing HTML. To use components with template renderers, you must use their HTML format. Templates are immutable, meaning the state of the components cannot be managed on the server side. The template can however have different representations depending on the state of the item.
The only data sent from the server, other than the template itself (which is only sent once), is the extra name property of each item.
Templates still enable event handling on the server side but you cannot, for example, disable or change the text of a button from the event handler. For those types of situations, use editors instead.
With template renderers, the server does not keep track of the components in each cell. It only manages the state of the item in each row. The client side does not need to wait for the server to send missing information about what needs to be rendered: it can use the template and stamp away all the information it needs.
Dynamic Height
Grid has a default height of 400 pixels. It becomes scrollable when its items overflow the allocated space.
In addition to setting any fixed or relative value, the height of a grid can be set by the number of items in the dataset, meaning that the grid will grow and shrink based on the row count.
Please note that this disables scrolling and should not be used for large data sets to avoid performance issues.
Selection
Selection is enabled by default. Grid supports single and multi-select. The former allows the user to select exactly one item while the latter enables multiple items to be selected.
Single Selection Mode
In single selection mode, the user can select and deselect rows by clicking anywhere on the row.
Columns
Column alignment, freezing (fixed position), grouping, headers & footers, visibility, and width can be configured. Users can be allowed to resize and reorder columns.
Column Alignment
Three different column alignments are supported: left (default), center and right.
Right align is useful when comparing numeric values as it helps with readability and scannability. Tabular numbers (if the font offers them) or a monospace font could be used to further improve digit alignment.
Column Freezing
Columns and column groups can be frozen to exclude them from scrolling a grid horizontally. This can be useful for keeping the most important columns always visible in a grid with a large number of columns.
Note
|
Freeze leftmost columns only
While it’s technically possible to freeze any column, this feature should primarily be used to freeze a certain number of columns starting with the leftmost one, leaving all remaining columns to the right unfrozen. |
Column Grouping
It is possible to group columns together. Grouped columns share a common header and footer. Use this feature to better visualise and organise related or hierarchical data.
Column Headers & Footers
Each column has its own customizable header and footer. A basic column header shows the name in plain text. Footers are empty and thus hidden by default. Both can contain rich content and components.
Column Visibility
Columns and column groups can be hidden. You can provide the user with a menu for toggling column visibilities, for example, using Menu Bar.
Allowing the user to hide columns is useful when only a subset of the columns are relevant to their task, and if there is a large number of columns.
Column Reordering & Resizing
Enabling the user to reorder columns is useful when they wish to compare data that is not adjacent by default. Grouped columns can only be reordered within their group.
Resizing is helpful when a column’s content does not fit and gets cut off or varies in length.
Column Width
All columns are the same width by default. You can set a specific width for any column, or allow the Grid to automatically set the width based on the contents.
Column widths can be fixed or non-fixed (default). Fixed width columns do not grow or shrink as the available space changes, while non-fixed width columns do.
In the following example, the first and last columns have fixed widths. The second column’s width is to set to be based on the content, while the third column takes up the remaining space.
Sorting
Any column can be made sortable. Enable sorting to allow the user to sort items alphabetically, numerically, by date, etc.
You can also sort columns that contain rich and/or custom content by defining which property to sort by. For example, you can have a column containing a person’s profile picture, name and email sorted by the person’s last name.
Sorting helps users find and analyze the data, so it’s generally recommended to enable it for all applicable columns, except in cases where the order of items is an essential part of the data itself (such as prioritized lists).
Filtering
Filtering allows the user to quickly find a specific item or subset of items. You can add filters to Grid columns or use external filter fields.
Place filters outside the grid when:
-
The filter is based on multiple columns.
-
A bigger field or more complex filter UI is needed, which wouldn’t comfortably fit in a column.
Item Details
Item details are expandable content areas that can be displayed below the regular content of a row, used to display more information about an item. By default, an item’s details are toggled by clicking on the item’s row.
The default toggle behavior can be replaced by programmatically toggling the details visibility, for example, from a button click.
Context Menu
You can use Context Menu to provide shortcuts to the user. It appears on right (default) or left click. In a mobile browser, a long press opens the menu.
Please note that using a context menu should not be the only way of accomplishing a task. The same functionality needs to be accessible elsewhere in the UI as well.
See Context Menu for more information.
Drag and Drop
Grid supports drag and drop, for example to reorder rows and to drag rows between grids.
Drop Mode
The drop mode of a grid determines where a drop can happen. Vaadin offers 4 different drop modes:
Drop Mode | Description |
---|---|
Drops can occur on the grid as a whole, not on top of or between individual rows. Use this option when the order is unimportant. | |
Drops can happen between rows. Use this mode when the order is important. | |
Drops can take place on top of rows. This is useful when creating relationships between items or moving an item into another item, for example placing a file inside a folder. | |
On Top or Between | Drops can occur on top of or between rows. |
Drag Rows Between Grids
Rows can be dragged from one grid to another, for example to move, copy or link items from different datasets.
Inline Editing
Grid can be configured to allow inline editing. Editing can be either buffered and non-buffered. Buffered means changes must be explicitly committed, while non-buffered automatically commit changes on blur (when a field loses focus).
Buffered
new tab
Source code
ValidationMessage.java
ValidationMessage.java
GridBufferedInlineEditor.java
Non-Buffered
In the example below, double-click a row to start editing. Press Escape, or click on a different row to stop editing.
new tab
Source code
ValidationMessage.java
ValidationMessage.java
GridUnbufferedInlineEditor.java
Styling Rows and Columns
You can style individual cells based on the data, for example, to highlight changes or important information.
The Java Flow API currently only allows to style the whole row, but not individual cells.
Theme Variants
Grid variants can reduce the white space inside the grid, adjust border and row highlight visibility, and control cell content overflow behavior.
Variants can be combined together freely.
Compact
The compact
theme variant makes a grid more dense by reducing the header and row heights, as well as the spacing between columns.
It is useful for displaying more information on-screen without having to scroll. It can also help improve scannability and comparability between rows.
No Row Border
This theme variant removes the horizontal row borders.
It is best suited for small datasets.
Parsing larger sets may be difficult unless paired with the row-stripes
theme variant.
Column Borders
You can add vertical borders between columns by using the column-borders
theme variant.
Data sets with a lot of columns packed tightly together, or where content gets truncated, can benefit from the additional separation that vertical borders bring.
Row Stripes
The row-stripes
theme produces a background color for every other row.
It can have a positive effect on scannability.
Related Components
Component | Usage recommendations |
---|---|
Component for creating, displaying, updating and deleting tabular data. | |
Component for showing and editing tabular data. | |
Component for showing hierarchical tabular data. | |
Lightweight component for lightweight, single-column lists. |
DCDFD246-69FE-41E3-A286-A09B4EAE2E6F