Grid
- Usage
- Flow Usage
- Content
- Dynamic Height
- Selection
- Columns
- Sorting
- Filtering
- Lazy Loading
- Item Details
- Context Menu
- Tooltips
- Drag and Drop
- Inline Editing (Java Only)
- Styling Rows & Columns
- Theme Variants
- Cell Focus
- Related Components
Grid is a component for showing tabular data.
new tab
Source code
grid-basic.ts
GridBasic.java
Person.java
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.
new tab
Source code
grid-content.ts
GridContent.java
Component Renderer vs. Lit Renderer (Flow Only)
As demonstrated in the previous example, custom content can be rendered using component renderers or Lit 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 produce up to 1,000 components that need to be managed. The more components you use in a component renderer, the greater the impact on performance.
Component renderers are very flexible and easy to use, but should be used with caution. They’re better suited as editors since only a single row can be edited at a time. They can also be used for detail rows.
Lit Renderer
Lit renderers will render quickly, but require you to write HTML. To use components with Lit renderers, you need to use their HTML format. Lit templates are immutable, meaning the state of the components can’t be managed on the server side. However, the template can have different representations, depending on the state of the item.
The only data sent from the server, other than the template itself — which is sent only once — is the extra name property of each item.
Lit templates still enable event handling on the server side. However, you can’t, for example, disable or change the text of a button from the event handler. For such situations, use editors instead.
With Lit renderers, the server doesn’t keep track of the components in each cell. It only manages the state of the item in each row. The client side doesn’t have to wait for the server to send missing information about what needs to be rendered. It can use the template to render all of the cells it needs.
For more in-depth information, see Using Lit Renderers with Grid.
Wrap Cell Content
Overflowing cell content is clipped or truncated by default. This variant makes the content wrap instead.
new tab
Source code
grid-wrap-cell-content.ts
GridWrapCellContent.java
Tooltips can also be used to display content that doesn’t fit into the cell.
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. The grid grows and shrinks based on the row count. This disables scrolling and shouldn’t be used for large data sets to avoid performance issues.
new tab
Source code
grid-dynamic-height.ts
GridDynamicHeight.java
Selection
Selection isn’t enabled by default. Grid supports single- and multi-select. Single-select allows the user to select only one item, while multi-select permits 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.
new tab
Source code
grid-single-selection-mode.ts
GridSingleSelectionMode.java
Multi-Select Mode
In multi-select mode, the user can use a checkbox column to select and deselect rows.
new tab
Source code
grid-multi-select-mode.ts
GridMultiSelectionMode.java
Columns
Column alignment, freezing (i.e., fixed positioning), grouping, headers and footers, visibility, and width can be configured. Users can be permitted to resize and reorder columns.
Column Alignment
Three different column alignments are supported: left (default), center, and right.
Right alignment 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.
new tab
Source code
grid-column-alignment.ts
GridColumnAlignment.java
Column Freezing
Columns and column groups can be frozen — made sticky — to exclude them from scrolling a grid, horizontally. This can be useful for keeping the most important columns always visible in a grid with many columns. Freezing columns at the end of the grid is useful, for example, for keeping row actions always visible.
new tab
Source code
grid-column-freezing.ts
GridColumnFreezing.java
Although it’s technically possible to freeze any column, it should be used primarily to freeze columns at the start or end of the grid, leaving the remaining columns unfrozen.
Column Grouping
It’s possible to group columns together. Grouped columns share a common header and footer. Use this feature to better visualize and organize related or hierarchical data.
new tab
Source code
grid-column-grouping.ts
GridColumnGrouping.java
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.
new tab
Source code
grid-column-header-footer.ts
GridColumnHeaderFooter.java
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 is relevant to their task, and if there are plenty of columns.
new tab
Source code
grid-column-visibility.ts
GridColumnVisibility.java
Column Reordering & Resizing
Enabling the user to reorder columns is useful when they want to compare data that isn’t adjacent by default. Grouped columns can only be reordered within their group.
Resizing is helpful when a column’s content doesn’t fit and is cut off or varies in length.
new tab
Source code
grid-column-reordering-resizing.ts
GridColumnReorderingResizing.java
Column Width
All columns are the same width by default. You can set a specific width for any column, or allow the Grid to set the width automatically based on the contents.
Column widths can be fixed or non-fixed (default). Fixed width columns don’t 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 set to be based on the content, while the third column takes up the remaining space.
new tab
Source code
grid-column-width.ts
GridColumnWidth.java
Sorting
Any column can be made sortable. Enable sorting to allow the user to sort items alphabetically, numerically, by date, etc.
For detailed information on how to configure sorting in Flow, see Column Sorting.
new tab
Source code
grid-sorting.ts
GridSorting.java
Sorting by Multiple Columns
The Grid can be sorted by multiple columns simultaneously by enabling multi-sort mode. When this is enabled, clicking a column header adds that column as a sort criterion instead of replacing the current sort column. A separate multi-sort on shift-click mode combines the two by multi-sorting only when the column header is clicked while holding shift. The order in which multi-sort criteria (columns) are evaluated is determined by the multi-sort priority setting.
new tab
Source code
grid-multisort.ts
GridMultiSort.java
Note
|
Shift-Click Multi-Sorting has Accessibility Issues
The multi-sort on shift-click mode is not recommended for applications for which accessibility is important. The feature is unlikely to work well with assistive technologies, and the lack of visual affordances makes it difficult to discover for sighted users.
|
Specifying the Sort Property
Columns with rich or custom content can be sorted by defining the property by which to sort. For example, you can have a column containing a person’s profile picture, name and email sorted by the person’s last name.
new tab
Source code
grid-rich-content-sorting.ts
GridRichContentSorting.java
Sorting helps users find and analyze data. Therefore, it’s recommended to enable sorting for all applicable columns. An exception would be where the order is an essential part of the data itself, such as with prioritized lists.
Filtering
Filtering allows the user to find quickly a specific item or subset of items. You can add filters to Grid columns or use external filter fields.
new tab
Source code
grid-column-filtering.ts
GridColumnFiltering.java
Place filters outside the grid when the filter is based on multiple columns, or when a bigger field or more complex filter UI is needed, one which wouldn’t fit well in a column.
new tab
Source code
grid-external-filtering.ts
GridExternalFiltering.java
Lazy Loading
When you want to display a list of items which would be quite large to load entirely into memory, or you want to load items from a database, data providers can be used to provide lazy loading through pagination.
The following example uses a data provider for lazy loading, sorting, and filtering items:
new tab
Source code
grid-data-provider.ts
GridDataProvider.java
PersonDataProvider.java
PersonFilter.java
To learn more about data providers, see Binding Items to Components (Java only).
Item Details
Item details are expandable content areas that can be displayed below the regular content of a row. They can be used to display more information about an item. By default, an item’s details are toggled by clicking on the item’s row.
new tab
Source code
grid-item-details.ts
GridItemDetails.java
The default toggle behavior can be replaced by programmatically toggling the details visibility, such as from a button click.
new tab
Source code
grid-item-details-toggle.ts
GridItemDetailsToggle.java
Tooltips can be used as a lightweight alternative to the item details panel.
Context Menu
You can use Context Menu to provide shortcuts for the user. It appears on a right-click by default or a left click. In a mobile browser, a long press opens the menu.
Using a context menu shouldn’t be the only way of accomplishing a task. The same functionality needs to be accessible elsewhere in the UI.
See Context Menu for more information.
new tab
Source code
grid-context-menu.ts
GridContextMenuExample.java
Tooltips
Tooltips on cells can be used for many different use cases: They can be used for providing additional details on the contents of a cell — if an item details panel would be overkill or otherwise undesirable. They can provide the full text of a cell if it’s too long to fit feasibly into the cell itself — if wrapping the cell contents is insufficient or otherwise undesirable. Or they can provide textual explanations for non-text content, such as status icons.
new tab
Source code
grid-tooltip-generator.ts
GridTooltipGenerator.java
See Tooltips documentation for details on tooltip configuration.
Drag and Drop
Grid supports drag-and-drop actions. For example, this feature might be used 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 four different drop modes:
Drop Mode | Description |
---|---|
Drops can occur on the grid as a whole, not on top of rows or between individual rows. Use this option when the order isn’t important. | |
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, such as placing a file inside a folder. | |
On Top or Between | Drops can occur on top of rows or between rows. |
Row Reordering
You can drag rows to reorder them.
new tab
Source code
grid-row-reordering.ts
GridRowReordering.java
Drag Rows between Grids
Rows can be dragged from one grid to another. You might use this feature to move, copy or link items from different datasets.
new tab
Source code
grid-drag-rows-between-grids.ts
GridDragRowsBetweenGrids.java
Drag and Drop Filters
Drag and drop filters determine which rows are draggable and which rows are valid drop targets. The filters function on a per row basis.
new tab
Source code
grid-drag-drop-filters.ts
GridDragDropFilters.java
Inline Editing (Java Only)
Grid can be configured to allow inline editing. Editing can be either buffered or 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
GridBufferedInlineEditor.java
ValidationMessage.java
ValidationMessage.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
GridUnbufferedInlineEditor.java
ValidationMessage.java
ValidationMessage.java
Styling Rows & Columns
You can style individual cells based on the data. You might do this, for example, to highlight changes or important information.
new tab
Source code
grid-styling.ts
vaadin-grid-styling.css
vaadin-grid-styling.css
GridStyling.java
vaadin-grid-styling.css
vaadin-grid-styling.css
Theme Variants
Grid variants can reduce the white space inside the grid, adjust the border and row to highlight visibility, and control cell content overflow behavior.
Variants can be combined together.
Compact
The compact
theme variant makes a grid more dense by reducing the header and row heights, as well as the spacing between columns.
This is useful for displaying more information on-screen without having to scroll. It can also help improve scannability and comparability between rows.
new tab
Source code
grid-compact.ts
GridCompact.java
No Border
The no-border
theme variant removes the outer border of the grid.
new tab
Source code
grid-no-border.ts
GridNoBorder.java
No Row Border
This theme variant removes the horizontal row borders. It’s best suited for small datasets. Parsing larger datasets may be difficult unless paired with the row-stripes
theme variant.
new tab
Source code
grid-no-row-border.ts
GridNoRowBorder.java
Column Borders
You can add vertical borders between columns by using the column-borders
theme variant. Datasets with a lot of columns packed tightly together, or where content is truncated, can benefit from the additional separation that vertical borders bring.
new tab
Source code
grid-column-borders.ts
GridColumnBorders.java
Row Stripes
The row-stripes
theme produces a background color for every other row. This can have a positive effect on scannability.
new tab
Source code
grid-row-stripes.ts
GridRowStripes.java
Cell Focus
Cells can be focused by clicking on a cell or with the keyboard.
The following keyboard shortcuts are available:
Tab | Switches focus between sections of the grid (i.e., header, body, footer). |
Left, Up, Right, and Down Arrow Keys | Moves focus between cells within a section of the grid. |
Page Up | Moves cell focus up by one page of visible rows. |
Page Down | Moves cell focus down by one page of visible rows. |
Home | Moves focus to the first cell in a row. |
End | Moves focus to the last cell in a row. |
The cell focus event can be used to get notified when the user changes focus between cells.
By default, the focus outline is only visible when using keyboard navigation. For illustrative purposes, the example below also uses custom styles to show the focus outline when clicking on cells:
new tab
Source code
grid-cell-focus.ts
vaadin-grid-cell-focus.css
vaadin-grid-cell-focus.css
GridCellFocus.java
vaadin-grid-cell-focus.css
vaadin-grid-cell-focus.css
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. |
AC63AABF-4102-4C3E-9776-A09DDC04EF37