Grid
Grid is many things, and perhaps the most versatile and powerful component in Vaadin. Like Table, it allows presenting and editing tabular data, but escapes many of Table’s limitations. Efficient lazy loading of data while scrolling greatly improves performance. Grid is scalable, mobile friendly, and extensible.
Overview
Grid is for displaying and editing tabular data laid out in rows and columns. At the top, a header can be shown, and a footer at the bottom. In addition to plain text, the header and footer can contain HTML and components. Having components in the header allows implementing filtering easily. The grid data can be sorted by clicking on a column header; shift-clicking a column header enables secondary sorting criteria.
A Grid
The data area can be scrolled both vertically and horizontally. The leftmost columns can be frozen, so that they are never scrolled out of the view. The data is loaded lazily from the server, so that only the visible data is loaded. The smart lazy loading functionality gives excellent user experience even with low bandwidth, such as mobile devices.
The grid data can be edited with a row-based editor after double-clicking a row. The fields are generated with a field factory, or set explicitly, and bound to data with a field group.
Grid is fully themeable with CSS and style names can be set for all grid elements. For data rows and cells, the styles can be generated with a row or cell style generator.
Finally, Grid is designed to be extensible and used just as well for client-side development - its GWT API is nearly identical to the server-side API, including data binding.
Differences to Table
In addition to core features listed above, Grid has the following API-level and functional differences to Table:
Grid is not a Container itself, even though it can be bound to a container data source. Consequently, columns are defined differently, and so forth.
Rows can be added with addRow() shorthand (during initialization) instead of addItem().
Use setHeightByRows() and setHeightMode() instead of setPageLength() to set the height in number of rows.
Grid does not extend AbstractSelect and is not a field, but has its own selection API. addSelectionListener() is called to define a SelectionListener. The listener also receives a collection of deselected items.
Grid does not support having all cells in editable mode, it only supports row-based editing, with a row mini-editor that allows saving or discarding the changes.
Grid has no generated columns. Instead, the container data source can be wrapped around a GeneratedPropertyContainer.
No column icons; you can implement them in a column with an ImageRenderer.
Components can not be shown in Grid cells; instead the much more efficient renderers can be used for the most common cases, and row editor for editing values.
Limited support for drag and drop: the user can drag columns to reorder them.
In addition, Grid has the following visual changes:
Multiple selection is indicated with check boxes in addition to highlighting.
Grid does not show the row loading indicator like Table does.
Binding to Data
Grid is normally used by binding it to a container data source, described in “Collecting Items in Containers”. The container must implement Container.Indexed interface. By default, it is bound to an IndexedContainer; Grid offers some shorthand methods to operate on the default container, as described later.
You can set the container in the constructor or with setContainerDataSource().
For example, if you have a collection of beans, you could wrap them in a Vaadin BeanContainer or BeanItemContainer, and bind to a Grid as follows
Java
// Have some data
Collection<Person> people = Lists.newArrayList(
new Person("Nicolaus Copernicus", 1543),
new Person("Galileo Galilei", 1564),
new Person("Johannes Kepler", 1571));
// Have a container of some type to contain the data
BeanItemContainer<Person> container =
new BeanItemContainer<Person>(Person.class, people);
// Create a grid bound to the container
Grid grid = new Grid(container);
grid.setColumnOrder("name", "born");
layout.addComponent(grid);
Note that you need to override equals() and hashcode() for the bean (Person) class to make the BeanItemContainer work properly.
Default Data Source and Shorthands
Sometimes, when you have just a few fixed items that you want to display, you can define the grid columns and add data rows manually. Grid is by default bound to a IndexedContainer. You can define new columns (container properties) with addColumn() and then add rows (items) with addRow(). The types in the row data must match the defined column types.
For example:
Java
// Create a grid
Grid grid = new Grid();
// Define some columns
grid.addColumn("name", String.class);
grid.addColumn("born", Integer.class);
// Add some data rows
grid.addRow("Nicolaus Copernicus", 1543);
grid.addRow("Galileo Galilei", 1564);
grid.addRow("Johannes Kepler", 1571);
layout.addComponent(grid);
Or, if you have the data in an array:
Java
// Have some data
Object[][] people = { {"Nicolaus Copernicus", 1543},
{"Galileo Galilei", 1564},
{"Johannes Kepler", 1571}};
for (Object[] person: people)
grid.addRow(person);
Note that you can not use addRow() to add items if the container is read-only or has read-only columns, such as generated columns.
Handling Selection Changes
Selection in Grid is handled a bit differently from other selection components, as it is not an AbstractSelect. Grid supports both single and multiple selection, defined by the selection mode. Selection events can be handled with a SelectionListener.
Selection Mode
A Grid can be set to be in SINGLE (default), MULTI, or NONE selection mode, defined in the Grid.SelectionMode enum.
Java
// Use single-selection mode (default)
grid.setSelectionMode(SelectionMode.SINGLE);
Empty (null) selection is allowed by default, but can be disabled with setDeselectAllowed() in single-selection mode.
The selection is handled with a different selection model object in each respective selection mode: SingleSelectionModel, MultiSelectionModel, and NoSelectionModel (in which selection is always empty).
Java
// Pre-select an item
SingleSelectionModel selection =
(SingleSelectionModel) grid.getSelectionModel();
selection.select( // Select 3rd item
grid.getContainerDataSource().getIdByIndex(2));
Handling Selection
Changes in the selection can be handled with a SelectionListener. You need to implement the select() method, which gets a SelectionEvent as parameter. In addition to selection, you can handle clicks on rows or cells with a ItemClickListener.
You can get the new selection from the selection event with getSelected(), which returns a set of item IDs, or more simply from the grid or the selection model with getSelectedRow(), which returns the single selected item ID.
For example:
Java
grid.addSelectionListener(selectionEvent -> { // Java 8
// Get selection from the selection model
Object selected = ((SingleSelectionModel)
grid.getSelectionModel()).getSelectedRow();
if (selected != null)
Notification.show("Selected " +
grid.getContainerDataSource().getItem(selected)
.getItemProperty("name"));
else
Notification.show("Nothing selected");
});
The current selection can be obtained from the Grid object by getSelectedRow() or getSelectedRows(), which return one (in single-selection mode) or all (in multi-selection mode) selected items.
Warning | Note that changes to the item set of the container data source are not automatically reflected in the selection model and may cause the selection model to refer to stale item IDs. This always occurs, for example, when you delete the selected item or items. So, if you modify the item set of the container, you should synchronize or reset the selection with the container, such as by calling reset() on the selection model. |
Multiple Selection
In the multiple selection mode, a user can select multiple items by clicking on the checkboxes in the leftmost column, or by using the Space to select/deselect the currently focused row. Space bar is the default key for toggling the selection, but it can be customized.
Multiple Selection in Grid
The selection is managed through the MultiSelectionMode class. The currently selected rows can be set with setSelected() by a collection of item IDs, or you can use select() to add items to the selection.
Java
// Grid in multi-selection mode
Grid grid = new Grid(exampleDataSource());
grid.setSelectionMode(SelectionMode.MULTI);
// Pre-select some items
MultiSelectionModel selection =
(MultiSelectionModel) grid.getSelectionModel();
selection.setSelected( // Items 2-4
grid.getContainerDataSource().getItemIds(2, 3));
The current selection can be read with getSelectedRows(); either in the MultiSelectionMode object or in the Grid.
Java
// Allow deleting the selected items
Button delSelected = new Button("Delete Selected", e -> {
// Delete all selected data items
for (Object itemId: selection.getSelectedRows())
grid.getContainerDataSource().removeItem(itemId);
// Otherwise out of sync with container
grid.getSelectionModel().reset();
// Disable after deleting
e.getButton().setEnabled(false);
});
delSelected.setEnabled(grid.getSelectedRows().size() > 0);
Changes in the selection can be handled with a SelectionListener. The selection event object provides getAdded() and getRemoved() to allow determining the differences in the selection change.
Java
// Handle selection changes
grid.addSelectionListener(selection -> { // Java 8
Notification.show(selection.getAdded().size() +
" items added, " +
selection.getRemoved().size() +
" removed.");
// Allow deleting only if there's any selected
deleteSelected.setEnabled(
grid.getSelectedRows().size() > 0);
});
Disallowing User Selection
It is possible to prevent the user from changing the selection in grid for both single- and multi-selection models:
Java
HasUserSelectionAllowed model = (HasUserSelectionAllowed) grid.getSelectionModel();
model.setUserSelectionAllowed(false);
Note | Both SingleSelectionModel and MultiSelectModel implement HasUserSelectionAllowed so the cast is generally safe. |
Focus and Clicks
In addition to selecting rows, you can focus individual cells. The focus can be moved with arrow keys and, if editing is enabled, pressing Enter opens the editor. Normally, pressing Tab or Shift+Tab moves the focus to another component, as usual.
When editing or in unbuffered mode, Tab or Shift+Tab moves the focus to the next or previous cell. The focus moves from the last cell of a row forward to the beginning of the next row, and likewise, from the first cell backward to the end of the previous row. Note that you can extend DefaultEditorEventHandler to change this behavior.
With the mouse, you can focus a cell by clicking on it. The clicks can be handled with an ItemClickListener. The ItemClickEvent object contains various information, most importantly the ID of the clicked row and column.
Java
grid.addItemClickListener(event -> // Java 8
Notification.show("Value: " +
container.getContainerProperty(event.getItemId(),
event.getPropertyId()).getValue().toString()));
The clicked grid cell is also automatically focused.
The focus indication is themed so that the focused cell has a visible focus indicator style by default, while the row doesn’t. You can enable row focus, as well as disable cell focus, in a custom theme. See Styling with CSS.
Configuring Columns
Columns are normally defined in the container data source. The addColumn() method can be used to add columns to a container that supports it, such as the default IndexedContainer.
Column configuration is defined in Grid.Column objects, which can be obtained from the grid with getColumn() by the column (property) ID.
Java
Grid.Column bornColumn = grid.getColumn("born");
bornColumn.setHeaderCaption("Born");
In the following, we describe the basic column configuration.
Column Order
You can set the order of columns with setColumnOrder() for the grid. Columns that are not given for the method are placed after the specified columns in their natural order.
Java
grid.setColumnOrder("firstname", "lastname", "born",
"birthplace", "died");
Note that the method can not be used to hide columns. You can hide columns with the removeColumn(), as described later, or by hiding them in a GeneratedPropertyContainer.
Hiding Columns
Columns can be hidden by removing them with removeColumn(). You can remove all columns with removeAllColumns(). The removed columns are only removed from the grid, not from the container data source.
To restore a previously removed column, you can call addColumn() with the property ID. Instead of actually adding another column to the data source, it merely restores the previously removed one. However, column settings such as header or editor are not restored, but must be redone.
You can also hide columns at container-level. At least GeneratedpropertyContainer allows doing so, as described in “GeneratedPropertyContainer”.
Column Captions
Column captions are displayed in the grid header. The default captions are generated automatically from the property ID. You can set the header caption explicitly through the column object with setHeaderCaption().
Java
Grid.Column bornColumn = grid.getColumn("born");
bornColumn.setHeaderCaption("Born");
This is equivalent to setting it with setText() for the header cell; the HeaderCell also allows setting the caption in HTML or as a component, as well as styling it, as described later in Header and Footer.
Column Widths
Columns have by default undefined width, which causes automatic sizing based on the widths of the displayed data. You can set column widths explicitly by pixel value with setWidth(), or relatively using expand ratios with setExpandRatio().
When using expand ratios, the columns with a non-zero expand ratio use the extra space remaining from other columns, in proportion to the defined ratios.
You can specify minimum and maximum widths for the expanding columns with setMinimumWidth() and setMaximumWidth(), respectively.
The user can resize columns by dragging their separators with the mouse. When resized manually, all the columns widths are set to explicit pixel values, even if they had relative values before.
Frozen Columns
You can set the number of columns to be frozen with setFrozenColumnCount(), so that they are not scrolled off when scrolling horizontally.
Java
grid.setFrozenColumnCount(2);
Setting the count to 0 disables frozen data columns; setting it to -1 also disables the selection column in multi-selection mode.
Generating Columns
Columns with values computed from other columns or in some other way can be generated with a container or data model that generates the property values. The GeneratedPropertyContainer can be used for this purpose. It wraps around any indexed container to extend its properties with read-only generated properties. The generated properties can have same IDs as the original ones, thereby replacing them with formatted or converted values. See “GeneratedPropertyContainer” for a detailed description of using it.
Generated columns are read-only, so you can not add grid rows with addRow(). In editable mode, editor fields are not generated for generated columns.
Note that, while GeneratedPropertyContainer implements Container.Sortable, the wrapped container might not, and also sorting on the generated properties requires special handling. In such cases, generated properties or the entire container might not actually be sortable.
Column Renderers
A renderer is a feature that draws the client-side representation of a data value. This allows having images, HTML, and buttons in grid cells.
Column renderers: image, date, HTML, and button
Renderers implement the Renderer interface. You set the column renderer in the Grid.Column object as follows:
Java
grid.addColumn("born", Integer.class);
...
Grid.Column bornColumn = grid.getColumn("born");
bornColumn.setRenderer(new NumberRenderer("born in %d AD"));
Renderers require a specific data type for the column. To convert to a property type to a type required by a renderer, you can pass an optional Converter to setRenderer(), as described later in this section. A converter can also be used to (pre)format the property values. The converter is run on the server-side, before sending the values to the client-side to be rendered with the renderer.
The following renderers are available, as defined in the server-side com.vaadin.ui.renderers package:
ButtonRenderer
Renders the data value as the caption of a button. A RendererClickListener can be given to handle the button clicks.
Typically, a button renderer is used to display buttons for operating on a data item, such as edit, view, delete, etc. It is not meaningful to store the button captions in the data source, rather you want to generate them, and they are usually all identical.
Java
BeanItemContainer<Person> people =
new BeanItemContainer<>(Person.class);
people.addBean(new Person("Nicolaus Copernicus", 1473));
people.addBean(new Person("Galileo Galilei", 1564));
people.addBean(new Person("Johannes Kepler", 1571));
// Generate button caption column
GeneratedPropertyContainer gpc =
new GeneratedPropertyContainer(people);
gpc.addGeneratedProperty("delete",
new PropertyValueGenerator<String>() {
@Override
public String getValue(Item item, Object itemId,
Object propertyId) {
return "Delete"; // The caption
}
@Override
public Class<String> getType() {
return String.class;
}
});
// Create a grid
Grid grid = new Grid(gpc);
// Render a button that deletes the data row (item)
grid.getColumn("delete")
.setRenderer(new ButtonRenderer(e -> // Java 8
grid.getContainerDataSource()
.removeItem(e.getItemId())));
ImageRenderer
Renders the cell as an image. The column type must be a Resource, as described in “Images and Other Resources”; only ThemeResource and ExternalResource are currently supported for images in Grid.
Java
grid.addColumn("picture", Resource.class)
.setRenderer(new ImageRenderer());
...
// Add some data rows
grid.addRow(new ThemeResource("img/copernicus-128px.jpg"),
"Nicolaus Copernicus", 1543);
grid.addRow(new ThemeResource("img/galileo-128px.jpg"),
"Galileo Galilei", 1564);
Instead of creating the resource objects explicitly, as was done above, you could generate them dynamically from file name strings using a Converter for the column.
Java
// Define some columns
grid.addColumn("picture", String.class); // Filename
grid.addColumn("name", String.class);
// Set the image renderer
grid.getColumn("picture").setRenderer(new ImageRenderer(),
new Converter<Resource, String>() {
@Override
public String convertToModel(Resource value,
Class<? extends String> targetType, Locale l)
throws Converter.ConversionException {
return "not needed";
}
@Override
public Resource convertToPresentation(String value,
Class<? extends Resource> targetType, Locale l)
throws Converter.ConversionException {
return new ThemeResource("img/" + value);
}
@Override
public Class<String> getModelType() {
return String.class;
}
@Override
public Class<Resource> getPresentationType() {
return Resource.class;
}
});
// Add some data rows
grid.addRow("copernicus-128px.jpg", "Nicolaus Copernicus");
grid.addRow("galileo-128px.jpg", "Galileo Galilei");
grid.addRow("kepler-128px.jpg", "Johannes Kepler");
You also need to define the row heights so that the images fit there. You can set it in the theme for all data cells or for the column containing the images.
For the latter way, first define a CSS style name for grid and the column:
Java
grid.setStyleName("gridwithpics128px");
grid.setCellStyleGenerator(cell ->
"picture".equals(cell.getPropertyId())?
"imagecol" : null);
Then, define the style in CSS (Sass):
CSS
.gridwithpics128px .imagecol {
height: 128px;
background: black;
text-align: center;
}
DateRenderer
Formats a column with a Date type using string formatter. The format string is same as for String.format() in Java API. The date is passed in the parameter index 1, which can be omitted if there is only one format specifier, such as “%tF”.
Java
Grid.Column bornColumn = grid.getColumn("born");
bornColumn.setRenderer(
new DateRenderer("%1$tB %1$te, %1$tY",
Locale.ENGLISH));
Optionally, a locale can be given. Otherwise, the default locale (in the component tree) is used.
HTMLRenderer
Renders the cell as HTML. This allows formatting cell content, as well as using HTML features such as hyperlinks.
First, set the renderer in the Grid.Column object:
Java
grid.addColumn("link", String.class)
.setRenderer(new HtmlRenderer());
Then, in the grid data, give the cell content:
Java
grid.addRow("Nicolaus Copernicus", 1543,
"<a href='http://en.wikipedia.org/wiki/" +
"Nicolaus_Copernicus' target='_top'>info</a>");
You could also use a PropertyFormatter or a generated column to generate the HTML for the links.
NumberRenderer
Formats column values with a numeric type extending Number: Integer, Double, etc. The format can be specified either by the subclasses of java.text.NumberFormat, namely DecimalFormat and ChoiceFormat, or by String.format().
For example:
Java
// Define some columns
grid.addColumn("name", String.class);
grid.addColumn("born", Integer.class);
grid.addColumn("sletters", Integer.class);
grid.addColumn("rating", Double.class);
// Use decimal format
grid.getColumn("born").setRenderer(new NumberRenderer(
new DecimalFormat("in #### AD")));
// Use textual formatting on numeric ranges
grid.getColumn("sletters").setRenderer(new NumberRenderer(
new ChoiceFormat("0#none|1#one|2#multiple")));
// Use String.format() formatting
grid.getColumn("rating").setRenderer(new NumberRenderer(
"%02.4f", Locale.ENGLISH));
// Add some data rows
grid.addRow("Nicolaus Copernicus", 1473, 2, 0.4);
grid.addRow("Galileo Galilei", 1564, 0, 4.2);
grid.addRow("Johannes Kepler", 1571, 1, 2.3);
ProgressBarRenderer
Renders a progress bar in a column with a Double type. The value must be between 0.0 and 1.0.
For example:
Java
// Define some columns
grid.addColumn("name", String.class);
grid.addColumn("rating", Double.class)
.setRenderer(new ProgressBarRenderer());
// Add some data rows
grid.addRow("Nicolaus Copernicus", 0.1);
grid.addRow("Galileo Galilei", 0.42);
grid.addRow("Johannes Kepler", 1.0);
TextRenderer
Displays plain text as is. Any HTML markup is quoted.
Custom Renderers
Renderers are component extensions that require a client-side counterpart. See “Renderers” for information on implementing custom renderers.
Converting for Rendering
Optionally, you can give a Converter in the setRenderer(), or define it for the column, to convert the data value to an intermediary representation that is rendered by the renderer. For example, when using an ImageRenderer, you could store the image file name in the data column, which the converter would convert to a resource, which would then be rendered by the renderer.
In the following example, we use a converter and HTMLRenderer to display boolean values as FontAwesome icons
Java
// Have a column for hyperlink paths to Wikipedia
grid.addColumn("truth", Boolean.class);
Grid.Column truth = grid.getColumn("truth");
truth.setRenderer(new HtmlRenderer(),
new StringToBooleanConverter(
FontAwesome.CHECK_CIRCLE_O.getHtml(),
FontAwesome.CIRCLE_O.getHtml()));
...
In this example, we use a converter to format URL paths to complete HTML hyperlinks with HTMLRenderer:
Java
// Have a column for hyperlink paths to Wikipedia
grid.addColumn("link", String.class);
Grid.Column linkColumn = grid.getColumn("link");
linkColumn.setRenderer(new HtmlRenderer(),
new Converter<String,String>(){
@Override
public String convertToModel(String value,
Class<? extends String> targetType, Locale locale)
throws Converter.ConversionException {
return "not implemented";
}
@Override
public String convertToPresentation(String value,
Class<? extends String> targetType, Locale locale)
throws Converter.ConversionException {
return "<a href='http://en.wikipedia.org/wiki/" +
value + "' target='_blank'>more info</a>";
}
@Override
public Class<String> getModelType() {
return String.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
});
// Data with a hyperlink path in the third column
grid.addRow("Nicolaus Copernicus", 1473,
"Nicolaus_Copernicus");
...
A GeneratedPropertyContainer could be used for much the same purpose.
Header and Footer
A grid by default has a header, which displays column names, and can have a footer. Both can have multiple rows and neighbouring header row cells can be joined to feature column groups.
Adding and Removing Header and Footer Rows
A new header row is added with prependHeaderRow(), which adds it at the top of the header, appendHeaderRow(), which adds it at the bottom of the header, or with addHeaderRowAt(), which inserts it at the specified 0-base index. All of the methods return a HeaderRow object, which you can use to work on the header further.
Java
// Group headers by joining the cells
HeaderRow groupingHeader = grid.prependHeaderRow();
...
// Create a header row to hold column filters
HeaderRow filterRow = grid.appendHeaderRow();
...
Similarly, you can add footer rows with appendFooterRow(), prependFooterRow(), and addFooterRowAt().
Joining Header and Footer Cells
You can join two or more header or footer cells with the join() method. For header cells, the intention is usually to create column grouping, while for footer cells, you typically calculate sums or averages.
Java
// Group headers by joining the cells
HeaderRow groupingHeader = grid.prependHeaderRow();
HeaderCell namesCell = groupingHeader.join(
groupingHeader.getCell("firstname"),
groupingHeader.getCell("lastname")).setText("Person");
HeaderCell yearsCell = groupingHeader.join(
groupingHeader.getCell("born"),
groupingHeader.getCell("died"),
groupingHeader.getCell("lived")).setText("Dates of Life");
Text and HTML Content
You can set the header caption with setText(), in which case any HTML formatting characters are quoted to ensure security.
Java
HeaderRow mainHeader = grid.getDefaultHeaderRow();
mainHeader.getCell("firstname").setText("First Name");
mainHeader.getCell("lastname").setText("Last Name");
mainHeader.getCell("born").setText("Born In");
mainHeader.getCell("died").setText("Died In");
mainHeader.getCell("lived").setText("Lived For");
To use raw HTML in the captions, you can use setHtml().
Java
namesCell.setHtml("<b>Names</b>");
yearsCell.setHtml("<b>Years</b>");
Components in Header or Footer
You can set a component in a header or footer cell with setComponent(). Often, this feature is used to allow filtering, as described in Filtering, which also gives an example of the use.
Filtering
The ability to include components in the grid header can be used to create filters for the grid data. Filtering is done in the container data source, so the container must be of type that implements Container.Filterable.
Filtering Grid
The filtering illustrated in Filtering Grid can be created as follows:
Java
// Have a filterable container
IndexedContainer container = exampleDataSource();
// Create a grid bound to it
Grid grid = new Grid(container);
grid.setSelectionMode(SelectionMode.NONE);
grid.setWidth("500px");
grid.setHeight("300px");
// Create a header row to hold column filters
HeaderRow filterRow = grid.appendHeaderRow();
// Set up a filter for all columns
for (Object pid: grid.getContainerDataSource()
.getContainerPropertyIds()) {
HeaderCell cell = filterRow.getCell(pid);
// Have an input field to use for filter
TextField filterField = new TextField();
filterField.setColumns(8);
// Update filter When the filter input is changed
filterField.addTextChangeListener(change -> {
// Can't modify filters so need to replace
container.removeContainerFilters(pid);
// (Re)create the filter if necessary
if (! change.getText().isEmpty())
container.addContainerFilter(
new SimpleStringFilter(pid,
change.getText(), true, false));
});
cell.setComponent(filterField);
}
Sorting
A user can sort the data in a grid on a column by clicking the column header. Clicking another time on the current sort column reverses the sort direction. Clicking on other column headers while keeping the Shift key pressed adds a secondary or more sort criteria.
Sorting Grid on Multiple Columns
Defining sort criteria programmatically can be done with the various alternatives of the sort() method. You can sort on a specific column with sort(Object propertyId), which defaults to ascending sorting order, or sort(Object propertyId, SortDirection direction), which allows specifying the sort direction.
Java
grid.sort("name", SortDirection.DESCENDING);
To sort on multiple columns, you need to use the fluid sort API with sort(Sort), which allows chaining sorting rules. Sorting rules are created with the static by() method, which defines the primary sort column, and then(), which can be used to specify any secondary sort columns. They default to ascending sort order, but the sort direction can be given with an optional parameter.
Java
// Sort first by city and then by name
grid.sort(Sort.by("city", SortDirection.ASCENDING)
.then("name", SortDirection.DESCENDING));
The container data source must support sorting. At least, it must implement Container.Sortable. Note that when using GeneratedPropertyContainer, as described in Generating Columns, even though the container implements the interface, the wrapped container must also support it. Also, the generated properties are not normally sortable, but require special handling to enable sorting.
Editing
Grid supports line-based editing, where double-clicking a row opens the row editor. In the editor, the input fields can be edited, as well as navigated with Tab and Shift+Tab keys. If validation fails, an error is displayed and the user can correct the inputs.
To enable editing, you need to call setEditorEnabled(true) for the grid.
Java
Grid grid = new Grid(GridExample.exampleDataSource());
grid.setEditorEnabled(true);
Grid supports two row editor modes - buffered and unbuffered. The default mode is buffered. The mode can be changed with setBuffered(false)
Buffered Mode
The editor has a Save button that commits the data item to the container data source and closes the editor. The Cancel button discards the changes and exits the editor.
A row under editing is illustrated in Editing a Grid Row.
Editing a Grid Row
Unbuffered Mode
The editor has no buttons and all changed data is committed directly to the container. If another row is clicked, the editor for the current row is closed and a row editor for the clicked row is opened.
Editor Fields
The editor fields are by default generated with a FieldFactory and bound to the container data source with a FieldGroup, which also handles tasks such as validation, as explained later.
To disable editing in a particular column, you can call setEditable() in the Column object with false parameter.
Customizing Editor Fields
The editor fields are normally created by the field factory of the editor’s field group, which creates the fields according to the data types of their respective columns. To customize the editor fields of specific properties, such as to style them or to set up validation, you can provide them with setEditorField() in the respective columns.
In the following example, we configure a field with validation and styling:
Java
TextField nameEditor = new TextField();
// Custom CSS style
nameEditor.addStyleName("nameeditor");
// Custom validation
nameEditor.addValidator(new RegexpValidator(
"^\\p{Alpha}+ \\p{Alpha}+$",
"Need first and last name"));
grid.getColumn("name").setEditorField(nameEditor);
Setting an editor field to null deletes the currently existing editor field, whether it was automatically generated or set explicitly with the setter. It will be regenerated with the factory the next time it is needed.
Customizing Editor Buttons
In the buffered mode, the editor has two buttons: Save and Cancel. You can set their captions with setEditorSaveCaption() and setEditorCancelCaption(), respectively.
In the following example, we demonstrate one way to translate the captions:
Java
// Captions are stored in a resource bundle
ResourceBundle bundle = ResourceBundle.getBundle(
MyAppCaptions.class.getName(),
Locale.forLanguageTag("fi")); // Finnish
// Localize the editor button captions
grid.setEditorSaveCaption(
bundle.getString(MyAppCaptions.SaveKey));
grid.setEditorCancelCaption(
bundle.getString(MyAppCaptions.CancelKey));
Binding to Data with a Field Group
Data binding to the item under editing is handled with a FieldGroup, which you need to set with setEditorFieldGroup. This is mostly useful when using special-purpose field groups, such as BeanFieldGroup to enable bean validation.
For example, assuming that we want to enable bean validation for a bean such as the following:
Java
public class Person implements Serializable {
@NotNull
@Size(min=2, max=10)
private String name;
@Min(1)
@Max(130)
private int age;
...]
We can now use a BeanFieldGroup in the Grid as follows:
Java
Grid grid = new Grid(exampleBeanDataSource());
grid.setColumnOrder("name", "age");
grid.setEditorEnabled(true);
// Enable bean validation for the data
grid.setEditorFieldGroup(
new BeanFieldGroup<Person>(Person.class));
// Have some extra validation in a field
TextField nameEditor = new TextField();
nameEditor.addValidator(new RegexpValidator(
"^\\p{Alpha}+ \\p{Alpha}+$",
"Need first and last name"));
grid.setEditorField("name", nameEditor);
To use bean validation as in the example above, you need to include an implementation of the Bean Validation API in the classpath, as described in “Bean Validation”.
Handling Validation Errors
The input fields are validated when the value is updated. The default error handler displays error indicators in the invalid fields, as well as the first error in the editor.
Editing a Grid Row
You can modify the error handling by implementing a custom EditorErrorHandler or by extending the DefaultEditorErrorHandler.
Editor Field Factory
The fields are generated by the FieldFactory of the field group; you can also set it with setEditorFieldFactory(). Alternatively, you can create the editor fields explicitly with setEditorField().
Programmatic Scrolling
You can scroll to first item with scrollToStart(), to end with scrollToEnd(), or to a specific row with scrollTo().
Generating Row or Cell Styles
You can style entire rows with a RowStyleGenerator or individual cells with a CellStyleGenerator.
Generating Row Styles
You set a RowStyleGenerator to a grid with setRowStyleGenerator(). The getStyle() method gets a RowReference, which contains various information about the row and a reference to the grid, and should return a style name or null if no style is generated.
For example, to add a style names to rows having certain values in one column, you can style them as follows:
Java
grid.setRowStyleGenerator(rowRef -> {// Java 8
if (! ((Boolean) rowRef.getItem()
.getItemProperty("alive")
.getValue()).booleanValue())
return "grayed";
else
return null;
});
You could then style the rows with CSS as follows:
CSS
.v-grid-row.grayed {
color: gray;
}
Generating Cell Styles
You set a CellStyleGenerator to a grid with setCellStyleGenerator(). The getStyle() method gets a CellReference, which contains various information about the cell and a reference to the grid, and should return a style name or null if no style is generated.
For example, to add a style name to a specific column, you can match on the property ID of the column as follows:
Java
grid.setCellStyleGenerator(cellRef -> // Java 8
"born".equals(cellRef.getPropertyId())?
"rightalign" : null);
You could then style the cells with a CSS rule as follows:
CSS
.v-grid-cell.rightalign {
text-align: right;
}
Styling with CSS
CSS
.v-grid {
.v-grid-scroller, .v-grid-scroller-horizontal { }
.v-grid-tablewrapper {
.v-grid-header {
.v-grid-row {
.v-grid-cell, .frozen, .v-grid-cell-focused { }
}
}
.v-grid-body {
.v-grid-row,
.v-grid-row-stripe,
.v-grid-row-has-data {
.v-grid-cell, .frozen, .v-grid-cell-focused { }
}
}
.v-grid-footer {
.v-grid-row {
.v-grid-cell, .frozen, .v-grid-cell-focused { }
}
}
}
.v-grid-header-deco { }
.v-grid-footer-deco { }
.v-grid-horizontal-scrollbar-deco { }
.v-grid-editor {
.v-grid-editor-cells { }
.v-grid-editor-footer {
.v-grid-editor-message { }
.v-grid-editor-buttons {
.v-grid-editor-save { }
.v-grid-editor-cancel { }
}
}
}
}
A Grid has an overall v-grid style. The actual grid has three parts: a header, a body, and a footer. The scrollbar is a custom element with v-grid-scroller style. In addition, there are some decoration elements.
Grid cells, whether thay are in the header, body, or footer, have a basic v-grid-cell style. Cells in a frozen column additionally have a frozen style. Rows have v-grid-row style, and every other row has additionally a v-grid-row-stripe style.
The focused row has additionally v-grid-row-focused style and focused cell v-grid-cell-focused. By default, cell focus is visible, with the border stylable with $v-grid-cell-focused-border parameter in Sass. Row focus has no visible styling, but can be made visible with the $v-grid-row-focused-background-color parameter or with a custom style rule.
In editing mode, a v-grid-editor overlay is placed on the row under editing. In addition to the editor field cells, it has an error message element, as well as the buttons.