Class Grid<T>

  • Type Parameters:
    T - the grid bean type
    All Implemented Interfaces:
    AttachNotifier, BlurNotifier<Grid<T>>, DetachNotifier, Focusable<Grid<T>>, FocusNotifier<Grid<T>>, HasElement, HasEnabled, HasSize, HasStyle, HasTheme, SortEvent.SortNotifier<Grid<T>,​GridSortOrder<T>>, HasDataGenerators<T>, HasDataView<T,​Void,​GridDataView<T>>, HasLazyDataView<T,​Void,​GridLazyDataView<T>>, HasListDataView<T,​GridListDataView<T>>, Serializable
    Direct Known Subclasses:
    CrudGrid, GridPro, TreeGrid

    @Tag("vaadin-grid")
    @NpmPackage(value="@vaadin/polymer-legacy-adapter",version="23.1.3") @NpmPackage(value="@vaadin/grid",version="23.1.3") @NpmPackage(value="@vaadin/vaadin-grid",version="23.1.3")
    @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") @JsModule("@vaadin/grid/src/vaadin-grid.js") @JsModule("@vaadin/grid/src/vaadin-grid-column.js") @JsModule("@vaadin/grid/src/vaadin-grid-sorter.js") @JsModule("@vaadin/checkbox/src/vaadin-checkbox.js") @JsModule("@vaadin/polymer-legacy-adapter/template-renderer.js") @JsModule("./flow-component-renderer.js") @JsModule("./gridConnector.js")
    public class Grid<T>
    extends Component
    implements HasStyle, HasSize, Focusable<Grid<T>>, SortEvent.SortNotifier<Grid<T>,​GridSortOrder<T>>, HasTheme, HasDataGenerators<T>, HasListDataView<T,​GridListDataView<T>>, HasDataView<T,​Void,​GridDataView<T>>, HasLazyDataView<T,​Void,​GridLazyDataView<T>>
    Grid is a component for showing tabular data. A basic Grid uses plain text to display information in rows and columns. However, rich content can be used to provide additional information in a more legible fashion using component renderers or Lit renderers. The Grid supports the following features.

    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.

    Selection:
    Grid selection is not 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. In single selection mode, the user can select and deselect rows by clicking anywhere on the row. In multi-select mode, the user can use a checkbox column to select and deselect rows.

    Columns:
    Column alignment, freezing (fixed position), grouping, headers & footers, visibility, and width can be configured. Users can be allowed to resize and reorder columns.

    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.

    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.

    Drag and Drop:
    Grid supports drag and drop, for example to reorder rows and to drag rows between grids.

    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).

    Styling Rows and Columns:
    You can style individual cells based on the data, for example, to highlight changes or important information.

    Cell Focus:
    Cells can be focused by clicking on a cell or with the keyboard.

    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Constructor Detail

      • Grid

        public Grid()
        Creates a new instance, with page size of 50.
      • Grid

        public Grid​(int pageSize)
        Creates a new instance, with the specified page size.

        The page size influences the Query.getLimit() sent by the client, but it's up to the webcomponent to determine the actual query limit, based on the height of the component and scroll position. Usually the limit is 3 times the page size (e.g. 150 items with a page size of 50).

        Parameters:
        pageSize - the page size. Must be greater than zero.
      • Grid

        public Grid​(Class<T> beanType,
                    boolean autoCreateColumns)
        Creates a new grid with an initial set of columns for each of the bean's properties. The property-values of the bean will be converted to Strings. Full names of the properties will be used as the column keys and the property captions will be used as the column headers. The generated columns will be sortable by default, if the property is Comparable.

        When autoCreateColumns is true, only the direct properties of the bean are included and they will be in alphabetical order. Use setColumns(String...) to define which properties to include and in which order. You can also add a column for an individual property with addColumn(String). Both of these methods support also sub-properties with dot-notation, eg. "property.nestedProperty".

        Parameters:
        beanType - the bean type to use, not null
        autoCreateColumns - when true, columns are created automatically for the properties of the beanType
      • Grid

        public Grid​(Class<T> beanType)
        Creates a new grid with an initial set of columns for each of the bean's properties. The property-values of the bean will be converted to Strings. Full names of the properties will be used as the column keys and the property captions will be used as the column headers. The generated columns will be sortable by default, if the property is Comparable.

        By default, only the direct properties of the bean are included and they will be in alphabetical order. Use setColumns(String...) to define which properties to include and in which order. You can also add a column for an individual property with addColumn(String). Both of these methods support also sub-properties with dot-notation, eg. "property.nestedProperty".

        Parameters:
        beanType - the bean type to use, not null
      • Grid

        protected Grid​(Class<T> beanType,
                       SerializableBiFunction<GridArrayUpdater.UpdateQueueData,​Integer,​Grid.UpdateQueue> updateQueueBuilder,
                       B dataCommunicatorBuilder)
        Creates a new grid with an initial set of columns for each of the bean's properties. The property-values of the bean will be converted to Strings. Full names of the properties will be used as the column keys and the property captions will be used as the column headers.

        You can add columns for nested properties of the bean with addColumn(String).

        Type Parameters:
        B - the data communicator builder type
        U - the GridArrayUpdater type
        Parameters:
        beanType - the bean type to use, not null
        updateQueueBuilder - the builder for new Grid.UpdateQueue instance
        dataCommunicatorBuilder - Builder for DataCommunicator implementation this Grid uses to handle all data communication.
      • Grid

        protected Grid​(int pageSize,
                       SerializableBiFunction<GridArrayUpdater.UpdateQueueData,​Integer,​Grid.UpdateQueue> updateQueueBuilder,
                       B dataCommunicatorBuilder)
        Creates a new instance, with the specified page size and data communicator.

        The page size influences the Query.getLimit() sent by the client, but it's up to the webcomponent to determine the actual query limit, based on the height of the component and scroll position. Usually the limit is 3 times the page size (e.g. 150 items with a page size of 50).

        Type Parameters:
        B - the data communicator builder type
        U - the GridArrayUpdater type
        Parameters:
        pageSize - the page size. Must be greater than zero.
        updateQueueBuilder - the builder for new Grid.UpdateQueue instance
        dataCommunicatorBuilder - Builder for DataCommunicator implementation this Grid uses to handle all data communication.
    • Method Detail

      • initConnector

        protected void initConnector()
      • getDefaultColumnFactory

        protected BiFunction<Renderer<T>,​String,​Grid.Column<T>> getDefaultColumnFactory()
        Gives a reference to the column factory.

        This method must not return null.

        Returns:
        method for column creation
      • addColumn

        public Grid.Column<T> addColumn​(Renderer<T> renderer,
                                        String... sortingProperties)
        Adds a new text column to this Grid with a template renderer, sorting properties and default column factory. The values inside the renderer are converted to JSON values by using JsonSerializer.toJson(Object).

        NOTE: You can add component columns easily using the addComponentColumn(ValueProvider), but using ComponentRenderer is not as efficient as the built in renderers or using TemplateRenderer.

        This constructor attempts to automatically configure both in-memory and backend sorting using the given sorting properties and matching those with the property names used in the given renderer.

        Note: if a property of the renderer that is used as a sorting property does not extend Comparable, no in-memory sorting is configured for it.

        Every added column sends data to the client side regardless of its visibility state. Don't add a new column at all or use removeColumn(Column) to avoid sending extra data.

        Note: This method is a shorthand for {@link ##addColumn(Renderer, BiFunction, String...)}

        Parameters:
        renderer - the renderer used to create the grid cell structure
        sortingProperties - the sorting properties to use for this column
        Returns:
        the created column
        See Also:
        getDefaultColumnFactory(), addColumn(Renderer, BiFunction, String...), removeColumn(Column)
      • addColumn

        protected <C extends Grid.Column<T>> C addColumn​(Renderer<T> renderer,
                                                         BiFunction<Renderer<T>,​String,​C> columnFactory,
                                                         String... sortingProperties)
        Adds a new text column to this Grid with a template renderer, sorting properties and column factory provided. The values inside the renderer are converted to JSON values by using JsonSerializer.toJson(Object).

        NOTE: You can add component columns easily using the addComponentColumn(ValueProvider), but using ComponentRenderer is not as efficient as the built in renderers or using TemplateRenderer.

        This constructor attempts to automatically configure both in-memory and backend sorting using the given sorting properties and matching those with the property names used in the given renderer.

        Note: if a property of the renderer that is used as a sorting property does not extend Comparable, no in-memory sorting is configured for it.

        Every added column sends data to the client side regardless of its visibility state. Don't add a new column at all or use removeColumn(Column) to avoid sending extra data.

        Parameters:
        renderer - the renderer used to create the grid cell structure
        columnFactory - the method that creates a new column instance for this Grid instance.
        sortingProperties - the sorting properties to use for this column
        Returns:
        the created column
        See Also:
        addColumn(Renderer, String...), removeColumn(Column)
      • addColumn

        protected <C extends Grid.Column<T>> C addColumn​(String propertyName,
                                                         BiFunction<Renderer<T>,​String,​C> columnFactory)
        Adds a new column for the given property name with the column factory provided. The property values are converted to Strings in the grid cells. The property's full name will be used as the column key and the property caption will be used as the column header.

        You can add columns for nested properties with dot notation, eg. "property.nestedProperty"

        If the property is Comparable, the created column is sortable by default. This can be changed with the Grid.Column.setSortable(boolean) method.

        Note: This method can only be used for a Grid created from a bean type with Grid(Class).

        Every added column sends data to the client side regardless of its visibility state. Don't add a new column at all or use removeColumn(Column) to avoid sending extra data.

        Parameters:
        propertyName - the property name of the new column, not null
        columnFactory - the method that creates a new column instance for this Grid instance.
        Returns:
        the created column
        See Also:
        addColumn(String), removeColumn(Column)
      • addColumns

        public void addColumns​(String... propertyNames)
        Adds a new columns for the given property names. The property values are converted to Strings in the grid cells. The properties' full names will be used as the column key and the properties' caption will be used as the column header.

        You can add columns for nested properties with dot notation, eg. "property.nestedProperty"

        If the property is Comparable, the created column is sortable by default. This can be changed with the Grid.Column.setSortable(boolean) method.

        Note: This method can only be used for a Grid created from a bean type with Grid(Class).

        Every added column sends data to the client side regardless of its visibility state. Don't add a new column at all or use removeColumn(Column) to avoid sending extra data.

        Parameters:
        propertyNames - the property names of the new columns, not null
        See Also:
        addColumn(String), removeColumn(Column)
      • setColumns

        public void setColumns​(String... propertyNames)
        Sets the columns and their order based on the given properties.

        This is a shortcut for removing all columns and then calling addColumn(String) for each of the given propertyNames.

        You can add columns for nested properties with dot notation, eg. "property.nestedProperty"

        Note that this also resets the headers and footers.

        Note: This method can only be used for a Grid created from a bean type with Grid(Class).

        Parameters:
        propertyNames - the properties to create columns for
      • setSortableColumns

        public void setSortableColumns​(String... propertyNames)
        Sets the defined columns as sortable, based on the given property names.

        This is a shortcut for setting all columns not sortable and then calling Grid.Column.setSortable(boolean) for each of the columns defined by the given propertyNames.

        You can set sortable columns for nested properties with dot notation, eg. "property.nestedProperty"

        Note: This method can only be used for a Grid created from a bean type with Grid(Class).

        Parameters:
        propertyNames - the property names used to reference the columns
        Throws:
        IllegalArgumentException - if any of the propertyNames refers to a non-existing column
        See Also:
        setColumns(String...), getColumnByKey(String)
      • setColumnKey

        protected void setColumnKey​(String key,
                                    Grid.Column column)
        Sets a user-defined identifier for given column.
        Parameters:
        column - the column
        key - the user-defined identifier
        See Also:
        Grid.Column.setKey(String)
      • createColumnId

        protected String createColumnId​(boolean increment)
      • prependHeaderRow

        public HeaderRow prependHeaderRow()
        Adds a new header row on the top of the existing header rows.

        If there are no existing header rows, this will create the first row.

        Returns:
        the created header row
      • appendHeaderRow

        public HeaderRow appendHeaderRow()
        Adds a new header row to the bottom of the existing header rows.

        If there are no existing header rows, this will create the first row.

        Returns:
        the created header row
      • addFirstHeaderRow

        protected HeaderRow addFirstHeaderRow()
      • getDefaultHeaderRow

        protected HeaderRow getDefaultHeaderRow()
      • prependFooterRow

        public FooterRow prependFooterRow()
        Adds a new footer row on the top of the existing footer rows.

        If there are no existing footer rows, this will create the first row.

        Returns:
        the created footer row
      • appendFooterRow

        public FooterRow appendFooterRow()
        Adds a new footer row to the bottom of the existing footer rows.

        If there are no existing footer rows, this will create the first row.

        Returns:
        the created header row
      • getColumnLayers

        protected List<com.vaadin.flow.component.grid.ColumnLayer> getColumnLayers()
      • getHeaderRows

        public List<HeaderRow> getHeaderRows()
        Gets all of the header rows in the Grid, in order from top to bottom.
        Returns:
        the header rows of the Grid
      • getFooterRows

        public List<FooterRow> getFooterRows()
        Gets all of the footer rows in the Grid, in order from top to bottom.
        Returns:
        the footer rows of the Grid
      • addThemeVariants

        public void addThemeVariants​(GridVariant... variants)
        Adds theme variants to the component.
        Parameters:
        variants - theme variants to add
      • removeThemeVariants

        public void removeThemeVariants​(GridVariant... variants)
        Removes theme variants from the component.
        Parameters:
        variants - theme variants to remove
      • insertColumnLayer

        protected com.vaadin.flow.component.grid.ColumnLayer insertColumnLayer​(int index,
                                                                               List<com.vaadin.flow.component.grid.AbstractColumn<?>> columns)
        Creates a new layer from the provided columns, inserts the layer into given index and returns the new layer.

        The user of this method should make sure that the DOM corresponds the column layer structure.

        Parameters:
        index - the index to insert
        columns - the column components that the new layer will wrap
        Returns:
        the new layer
      • removeColumnLayer

        protected void removeColumnLayer​(com.vaadin.flow.component.grid.ColumnLayer layer)
        Removes the given layer and moves the columns on the lower level to its place.
        Parameters:
        layer - the layer to remove, not the bottom layer
      • getDataProvider

        public DataProvider<T,​?> getDataProvider()
        Returns the data provider of this grid.

        To get information and control over the items in the grid, use either getListDataView() or getLazyDataView() instead.

        Returns:
        the data provider of this grid, not null
      • getDataCommunicator

        public DataCommunicator<T> getDataCommunicator()
        Returns the data communicator of this Grid.
        Returns:
        the data communicator, not null
      • getPageSize

        public int getPageSize()
        Gets the current page size, which is the number of items fetched at a time from the dataprovider.
        Returns:
        the current page size
      • setPageSize

        public void setPageSize​(int pageSize)
        Sets the page size, which is the number of items fetched at a time from the data provider. With the default value of 50, the grid might fetch items for example as: 0-49, 50-149, 150-200....

        Note: the number of items in the server-side memory can be considerably higher than the page size, since the component can show more than one page at a time.

        Setting the pageSize after the Grid has been rendered effectively resets the component, and the current page(s) and sent over again.

        With automatically extending grid, controlling the item count and how much it is increased when scrolling is possible via getLazyDataView().

        Parameters:
        pageSize - the maximum number of items sent per request. Should be greater than zero
      • getSelectionModel

        public GridSelectionModel<T> getSelectionModel()
        Returns the selection model for this grid.
        Returns:
        the selection model, not null
      • setSelectionModel

        protected void setSelectionModel​(GridSelectionModel<T> model,
                                         Grid.SelectionMode selectionMode)
        Sets the selection model for the grid.

        This method is for setting a custom selection model, and is protected because setSelectionMode(SelectionMode) should be used for easy switching between built-in selection models.

        The default selection model is GridSingleSelectionModel.

        To use a custom selection model, you can e.g. extend the grid call this method with your custom selection model.

        Parameters:
        model - the selection model to use, not null
        selectionMode - the selection mode this selection model corresponds to, not null
        See Also:
        setSelectionMode(SelectionMode)
      • updateSelectionModeOnClient

        protected void updateSelectionModeOnClient()
      • getSelectedItems

        public Set<T> getSelectedItems()
        This method is a shorthand that delegates to the currently set selection model.
        Returns:
        a set with the selected items, never null
        See Also:
        getSelectionModel(), GridSelectionModel
      • select

        public void select​(T item)
        This method is a shorthand that delegates to the currently set selection model.
        Parameters:
        item - the item to select, not null
        See Also:
        getSelectionModel(), GridSelectionModel
      • deselect

        public void deselect​(T item)
        This method is a shorthand that delegates to the currently set selection model.
        Parameters:
        item - the item to deselect, not null
        See Also:
        getSelectionModel(), GridSelectionModel
      • setItemDetailsRenderer

        public void setItemDetailsRenderer​(Renderer<T> renderer)
        Set the renderer to use for displaying the item details rows in this grid.
        Parameters:
        renderer - the renderer to use for displaying item details rows, null to remove the current renderer
      • isColumnReorderingAllowed

        @Synchronize("column-reordering-allowed-changed")
        public boolean isColumnReorderingAllowed()
        Returns whether column reordering is allowed. Default value is false.
        Returns:
        true if reordering is allowed
      • setColumnReorderingAllowed

        public void setColumnReorderingAllowed​(boolean columnReorderingAllowed)
        Sets whether or not column reordering is allowed. Default value is false.
        Parameters:
        columnReorderingAllowed - specifies whether column reordering is allowed
      • getColumns

        public List<Grid.Column<T>> getColumns()
        Gets an unmodifiable list of all Grid.Columns currently in this Grid.

        Note: If column reordering is enabled with setColumnReorderingAllowed(boolean) and the user has reordered the columns, the order of the list returned by this method might not be correct.

        Returns:
        unmodifiable list of columns
      • getColumnByKey

        public Grid.Column<T> getColumnByKey​(String columnKey)
        Gets a Grid.Column of this grid by its key.
        Parameters:
        columnKey - the identifier key of the column to get
        Returns:
        the column corresponding to the given column key, or null if no column has such key
        See Also:
        Grid.Column.setKey(String)
      • removeColumnByKey

        public void removeColumnByKey​(String columnKey)
        Removes a column with the given column key from the Grid.
        Parameters:
        columnKey - the key of the column, assigned by Grid.Column.setKey(String), or automatically created when using Grid(Class). Cannot be null
        Throws:
        IllegalArgumentException - if the column is not part of this Grid
      • removeColumns

        public void removeColumns​(Grid.Column<T>... columns)
        Removes columns from the Grid. Does nothing if the array is empty.
        Parameters:
        columns - the columns to be removed, not null
        Throws:
        NullPointerException - if the column is null
        IllegalArgumentException - if the column is not owned by this Grid
      • removeAllColumns

        public void removeAllColumns()
        Removes all columns from this Grid.
      • setDetailsVisible

        public void setDetailsVisible​(T item,
                                      boolean visible)
        Sets the visibility of details component for given item.
        Parameters:
        item - the item to show details for
        visible - true if details component should be visible; false if it should be hidden
      • setDetailsVisibleOnClick

        public void setDetailsVisibleOnClick​(boolean detailsVisibleOnClick)
        Sets whether the item details can be opened and closed by clicking the rows or not.
        Parameters:
        detailsVisibleOnClick - true to enable opening and closing item details by clicking the rows, false to disable this functionality
        See Also:
        setItemDetailsRenderer(Renderer)
      • isDetailsVisibleOnClick

        public boolean isDetailsVisibleOnClick()
        Gets whether the item details are opened and closed by clicking the rows or not.
        Returns:
        true if clicking the rows opens and closes their item details, false otherwise
        See Also:
        setItemDetailsRenderer(Renderer)
      • isDetailsVisible

        public boolean isDetailsVisible​(T item)
        Returns the visibility of details component for given item.
        Parameters:
        item - the item to show details for
        Returns:
        true if details component should be visible; false if it should be hidden
      • setMultiSort

        public void setMultiSort​(boolean multiSort)
        Sets whether multiple column sorting is enabled on the client-side.
        Parameters:
        multiSort - true to enable sorting of multiple columns on the client-side, false to disable
      • isMultiSort

        public boolean isMultiSort()
        Gets whether multiple column sorting is enabled on the client-side.
        Returns:
        true if sorting of multiple columns is enabled, false otherwise
        See Also:
        setMultiSort(boolean)
      • addContextMenu

        public GridContextMenu<T> addContextMenu()
        Adds a new context-menu for this grid.
        Returns:
        the added context-menu
      • sort

        public void sort​(List<GridSortOrder<T>> order)
        Forces a defined sort order for the columns in the Grid. Setting null or an empty list resets the ordering of all columns. Columns not mentioned in the list are reset to the unsorted state.

        For Grids with multi-sorting, the index of a given column inside the list defines the sort priority. For example, the column at index 0 of the list is sorted first, then on the index 1, and so on.

        When Grid is not configured to have multi-sorting enabled, all the columns in the list except the first one are ignored.

        Parameters:
        order - the list of sort orders to set on the client, or null to reset any sort orders.
        See Also:
        setMultiSort(boolean), getSortOrder()
      • onAttach

        protected void onAttach​(AttachEvent attachEvent)
        Description copied from class: Component
        Called when the component is attached to a UI.

        The default implementation does nothing.

        This method is invoked before the AttachEvent is fired for the component.

        Overrides:
        onAttach in class Component
        Parameters:
        attachEvent - the attach event
      • onDetach

        protected void onDetach​(DetachEvent detachEvent)
        Description copied from class: Component
        Called when the component is detached from a UI.

        The default implementation does nothing.

        This method is invoked before the DetachEvent is fired for the component.

        Overrides:
        onDetach in class Component
        Parameters:
        detachEvent - the detach event
      • getSortOrder

        public List<GridSortOrder<T>> getSortOrder()
        Gets an list of the current sort orders in the Grid.
        Returns:
        an unmodifiable list of sort orders
      • createSortingComparator

        protected SerializableComparator<T> createSortingComparator()
        Creates a comparator for grid to sort rows.
        Returns:
        the comparator based on column sorting information.
      • setHeightByRows

        @Deprecated
        public void setHeightByRows​(boolean heightByRows)
        Deprecated.
        If true, the grid's height is defined by its rows. All items are fetched from the DataProvider, and the Grid shows no vertical scroll bar.

        Note: setHeightByRows disables the grid's virtual scrolling so that all the rows are rendered in the DOM at once. If the grid has a large number of items, using the feature is discouraged to avoid performance issues.

        Parameters:
        heightByRows - true to make Grid compute its height by the number of rows, false for the default behavior
        See Also:
        setAllRowsVisible(boolean)
      • isHeightByRows

        @Deprecated
        public boolean isHeightByRows()
        Deprecated.
        since 14.7 - use isAllRowsVisible()
        Gets whether grid's height is defined by the number of its rows.
        Returns:
        true if Grid computes its height by the number of rows, false otherwise
        See Also:
        isAllRowsVisible()
      • setAllRowsVisible

        public void setAllRowsVisible​(boolean allRowsVisible)
        If true, the grid's height is defined by its rows. All items are fetched from the DataProvider, and the Grid shows no vertical scroll bar.

        Note: setAllRowsVisible disables the grid's virtual scrolling so that all the rows are rendered in the DOM at once. If the grid has a large number of items, using the feature is discouraged to avoid performance issues.

        Parameters:
        allRowsVisible - true to make Grid compute its height by the number of rows, false for the default behavior
      • isAllRowsVisible

        @Synchronize("all-rows-visible-changed")
        public boolean isAllRowsVisible()
        Gets whether grid's height is defined by the number of its rows.
        Returns:
        true if Grid computes its height by the number of rows, false otherwise
      • onEnabledStateChanged

        public void onEnabledStateChanged​(boolean enabled)
        Description copied from class: Component
        Handle component enable state when the enabled state changes.

        By default this sets or removes the 'disabled' attribute from the element. This can be overridden to have custom handling.

        Overrides:
        onEnabledStateChanged in class Component
        Parameters:
        enabled - the new enabled state of the component
      • addValueProvider

        public Registration addValueProvider​(String property,
                                             ValueProvider<T,​?> valueProvider)
        Adds a ValueProvider to this Grid that is not tied to a Column. This is specially useful when the columns are defined via a template file instead of the Java API.

        The properties added to by this method are global to the Grid - they can be used in any column.

        ValueProviders are registered as DataGenerators in the Grid. See addDataGenerator(DataGenerator).

        Parameters:
        property - the property name used in the template. For example, in a template the uses [[item.name]], the property is name. Not null
        valueProvider - the provider for values for the property, not null
        Returns:
        a registration that can be used to remove the ValueProvider from the Grid
      • addDataGenerator

        public Registration addDataGenerator​(DataGenerator<T> dataGenerator)
        Description copied from interface: HasDataGenerators
        Adds the given data generator. If the generator was already added, does nothing.
        Specified by:
        addDataGenerator in interface HasDataGenerators<T>
        Parameters:
        dataGenerator - the data generator to add
        Returns:
        a registration that can be used to remove the data generator
      • compareMaybeComparables

        protected static int compareMaybeComparables​(Object a,
                                                     Object b)
      • configureBeanType

        public void configureBeanType​(Class<T> beanType,
                                      boolean autoCreateColumns)
        Sets the bean type this grid is bound to and optionally adds a set of columns for each of the bean's properties. The property-values of the bean will be converted to Strings. Full names of the properties will be used as the column keys and the property captions will be used as the column headers. The generated columns will be sortable by default, if the property is Comparable.

        When autoCreateColumns is true, only the direct properties of the bean are included and they will be in alphabetical order. Use setColumns(String...) to define which properties to include and in which order. You can also add a column for an individual property with addColumn(String). Both of these methods support also sub-properties with dot-notation, eg. "property.nestedProperty".

        This method can only be called for a newly instanced Grid without any beanType or columns set.

        Parameters:
        beanType - the bean type to use, not null
        autoCreateColumns - when true, columns are created automatically for the properties of the beanType
      • getBeanType

        public Class<T> getBeanType()
        Returns the Class of bean this Grid is constructed with via Grid(Class). Or null if not constructed from a bean type.
        Returns:
        the Class of bean this Grid is constructed with
      • getPropertySet

        public PropertySet<T> getPropertySet()
        Returns PropertySet of bean this Grid is constructed with via Grid(Class). Or null if not constructed from a bean type.
        Returns:
        the PropertySet of bean this Grid is constructed with
      • addColumnResizeListener

        public Registration addColumnResizeListener​(ComponentEventListener<ColumnResizeEvent<T>> listener)
        Adds a column resize listener to this component. Note that the listener will be notified only for user-initiated column resize actions.
        Parameters:
        listener - the listener to add, not null
        Returns:
        a handle that can be used for removing the listener
      • addItemDoubleClickListener

        public Registration addItemDoubleClickListener​(ComponentEventListener<ItemDoubleClickEvent<T>> listener)
        Adds an item double click listener to this component.

        Note that double click event happens along with a click event. It means there is no way to get a double click event only (double click without a click): a click listener added using addItemClickListener(ComponentEventListener) (if any) will also be notified about a click event once a double click event is fired.

        Double click event type is not fully supported by the mobile browsers which means that double click event might not work (double click listeners won't be notified) for such browsers.

        Parameters:
        listener - the listener to add, not null
        Returns:
        a handle that can be used for removing the listener
        See Also:
        addItemClickListener(ComponentEventListener)
      • addCellFocusListener

        public Registration addCellFocusListener​(ComponentEventListener<CellFocusEvent<T>> listener)
        Adds a listener to the grid that will be notified, when a cell has been focused.

        The listener will be notified, when
        • the navigation focus of a cell gets activated
        • the focus is restored to the browser if a cell had navigation focus before the focus was lost
        • the navigation focus moves between header/body/footer sections

        The listener will not be notified, when
        • the focus changes between focusable elements in the Grid cells ("interaction mode")
        • on Grid Pro edit mode navigation ("interaction mode")
        • the focus changes between focusable elements in the cells in Flow Grid's editor mode ("interaction mode")
        Parameters:
        listener - the listener to add, not null
        Returns:
        a handle that can be used for removing the listener
      • setVerticalScrollingEnabled

        public void setVerticalScrollingEnabled​(boolean enabled)
        Enables or disables the vertical scrolling on the Grid web component. By default, the scrolling is enabled.
        Parameters:
        enabled - true to enable vertical scrolling, false to disabled it
      • isVerticalScrollingEnabled

        public boolean isVerticalScrollingEnabled()
        Gets whether the vertical scrolling on the Grid web component is enabled.
        Returns:
        true if the vertical scrolling is enabled, false otherwise
      • setClassNameGenerator

        public void setClassNameGenerator​(SerializableFunction<T,​String> classNameGenerator)
        Sets the function that is used for generating CSS class names for all the cells in the rows in this grid. Returning null from the generator results in no custom class name being set. Multiple class names can be returned from the generator as space-separated.

        If Grid.Column.setClassNameGenerator(SerializableFunction) is used together with this method, resulting class names from both methods will be effective. Class names generated by grid are applied to the cells before the class names generated by column. This means that if the classes contain conflicting style properties, column's classes will win.

        Parameters:
        classNameGenerator - the class name generator to set, not null
        Throws:
        NullPointerException - if classNameGenerator is null
        See Also:
        Grid.Column.setClassNameGenerator(SerializableFunction)
      • recalculateColumnWidths

        public void recalculateColumnWidths()
        Updates the width of all columns which have autoWidth set to true.
        See Also:
        Grid.Column.setAutoWidth(boolean)
      • getClassNameGenerator

        public SerializableFunction<T,​String> getClassNameGenerator()
        Gets the function that is used for generating CSS class names for rows in this grid.
        Returns:
        the class name generator
      • getUniqueKeyProvider

        protected ValueProvider<T,​String> getUniqueKeyProvider()
        Gets optional value provider for unique key in row's generated JSON.
        Returns:
        ValueProvider for unique key for row or null if not set
      • setUniqueKeyProvider

        protected void setUniqueKeyProvider​(ValueProvider<T,​String> uniqueKeyProvider)
        Sets value provider for unique key in row's generated JSON.

        null by default.

        Parameters:
        uniqueKeyProvider - ValueProvider for unique key for row
      • getUniqueKeyProperty

        protected String getUniqueKeyProperty()
        Gets property name for unique key in row's generated JSON.
        Returns:
        the optional property name for unique key
      • setUniqueKeyProperty

        protected void setUniqueKeyProperty​(String uniqueKeyProperty)
        Sets property name for unique key in row's generated JSON.
        Parameters:
        uniqueKeyProperty - the new optional property name for unique key
      • addDropListener

        public Registration addDropListener​(ComponentEventListener<GridDropEvent<T>> listener)
        Adds a drop listener to this component.
        Parameters:
        listener - the listener to add, not null
        Returns:
        a handle that can be used for removing the listener
      • addDragStartListener

        public Registration addDragStartListener​(ComponentEventListener<GridDragStartEvent<T>> listener)
        Adds a drag start listener to this component.
        Parameters:
        listener - the listener to add, not null
        Returns:
        a handle that can be used for removing the listener
      • addDragEndListener

        public Registration addDragEndListener​(ComponentEventListener<GridDragEndEvent<T>> listener)
        Adds a drag end listener to this component.
        Parameters:
        listener - the listener to add, not null
        Returns:
        a handle that can be used for removing the listener
      • setDropMode

        public void setDropMode​(GridDropMode dropMode)
        Sets the drop mode of this drop target. When set to not null, grid fires drop events upon data drop over the grid or the grid rows.

        When using GridDropMode.ON_TOP, and the grid is either empty or has empty space after the last row, the drop can still happen on the empty space, and the GridDropEvent.getDropTargetItem() will return an empty optional.

        When using GridDropMode.BETWEEN or GridDropMode.ON_TOP_OR_BETWEEN, and there is at least one row in the grid, any drop after the last row in the grid will get the last row as the GridDropEvent.getDropTargetItem(). If there are no rows in the grid, then it will return an empty optional.

        If using GridDropMode.ON_GRID, then the drop will not happen on any row, but instead just "on the grid". The target row will not be present in this case.

        NOTE: Prefer not using a row specific GridDropMode with a grid that enables sorting. If for example a new row gets added to a specific location on drop event, it might not end up in the location of the drop but rather where the active sorting configuration prefers to place it. This behavior might feel unexpected for the users.

        Parameters:
        dropMode - Drop mode that describes the allowed drop locations within the Grid's row. Can be null to disable dropping on the grid.
        See Also:
        GridDropEvent.getDropLocation()
      • getDropMode

        public GridDropMode getDropMode()
        Gets the drop mode of this drop target.
        Returns:
        Drop mode that describes the allowed drop locations within the Grid's row. null if dropping is not enabled.
      • setRowsDraggable

        public void setRowsDraggable​(boolean rowsDraggable)
        Sets whether the user can drag the grid rows or not.
        Parameters:
        rowsDraggable - true if the rows can be dragged by the user; false if not
      • isRowsDraggable

        public boolean isRowsDraggable()
        Gets whether rows of the grid can be dragged.
        Returns:
        true if the rows are draggable, false otherwise
      • getDropFilter

        public SerializablePredicate<T> getDropFilter()
        Gets the active drop filter.
        Returns:
        The drop filter function
      • getDragFilter

        public SerializablePredicate<T> getDragFilter()
        Gets the active drag filter.
        Returns:
        The drag filter function
      • setDropFilter

        public void setDropFilter​(SerializablePredicate<T> dropFilter)
        Sets the drop filter for this drag target.

        When the drop mode of the grid has been set to one of GridDropMode.BETWEEN, GridDropMode.ON_TOP or GridDropMode.ON_TOP_OR_BETWEEN, by default all the visible rows can be dropped over.

        A drop filter function can be used to specify the rows that are available for dropping over. The function receives an item and should return true if the row can be dropped over, false otherwise.

        NOTE: If the filter conditions depend on a specific row that's currently being dragged, you might want to have the grid's drop mode disabled by default and set its value only on drag start to avoid the small period of time during which the user might be able to drop over unwanted rows. Once the drop end event occurs, the drop mode can be set back to null to keep this consistent.

        NOTE: If the filtering conditions change dynamically, remember to explicitly invoke getDataProvider().refreshItem(item) for the relevant items to get the filters re-run for them.

      • setDragFilter

        public void setDragFilter​(SerializablePredicate<T> dragFilter)
        Sets the drag filter for this drag source.

        When the setRowsDraggable(boolean) has been used to enable dragging, by default all the visible rows can be dragged.

        A drag filter function can be used to specify the rows that are available for dragging. The function receives an item and returns true if the row can be dragged, false otherwise.

        NOTE: If the filtering conditions change dynamically, remember to explicitly invoke getDataProvider().refreshItem(item) for the relevant items to get the filters re-run for them.

      • setDragDataGenerator

        public void setDragDataGenerator​(String type,
                                         SerializableFunction<T,​String> dragDataGenerator)
        Sets a generator function for customizing drag data. The generated value will be accessible using the same type as the generator is set here. The function is executed for each item in the Grid during data generation. Return a String to be appended to the row as type data.

        Note that IE11 only supports data type "text"

        Parameters:
        type - Type of the generated data. The generated value will be accessible during drop using this type.
        dragDataGenerator - Function to be executed on row data generation.
      • setSelectionDragDetails

        public void setSelectionDragDetails​(int draggedItemsCount,
                                            Map<String,​String> dragData)
        Sets explicit drag operation details for when the user is dragging the selected items. By default, the drag data only covers the items in the visible viewport and all the items outside of it, even if selected, are excluded. Use this method to override the default drag data and the number shown in drag image on selection drag.

        Note that IE11 only supports data type "text"

        Parameters:
        draggedItemsCount - The number shown in the drag image on selection drag. Only values above 1 have any visible effect.
        dragData - The drag data for selection drag. The map should consist of data type:data -entries
      • addColumnReorderListener

        public Registration addColumnReorderListener​(ComponentEventListener<ColumnReorderEvent<T>> listener)
        Adds a column reorder listener to this component.
        Parameters:
        listener - the listener to add, not null
        Returns:
        a handle that can be used for removing the listener
      • setColumnOrder

        public void setColumnOrder​(Grid.Column<T>... columns)
        Sets a new column order for the grid.

        The function doesn't support column removal: all columns must be present in the list, otherwise IllegalArgumentException is thrown.

        The getColumns() function will reflect the new column ordering.

        Fires the ColumnReorderEvent with ComponentEvent.isFromClient() returning false.

        The method is atomic: if the requested reordering is not achievable, the function fails cleanly with IllegalArgumentException without doing any work.

        Parameters:
        columns - the new ordering of the columns, not null.
        Throws:
        NullPointerException - if the columns parameter is null.
        IllegalArgumentException - if a column is present two times in the list, or if the column is not owned by this Grid, or if the list doesn't contain all columns currently present in the Grid, or if the column rearranging would require to split a joined header/footer cell group.
        See Also:
        setColumnOrder(List)
      • setColumnOrder

        public void setColumnOrder​(List<Grid.Column<T>> columns)
        Sets a new column order for the grid.

        The function doesn't support column removal: all columns must be present in the list, otherwise IllegalArgumentException is thrown.

        The getColumns() function will reflect the new column ordering.

        Fires the ColumnReorderEvent with ComponentEvent.isFromClient() returning false.

        The method is atomic: if the requested reordering is not achievable, the function fails cleanly with IllegalArgumentException without doing any work.

        Parameters:
        columns - the new ordering of the columns, not null.
        Throws:
        NullPointerException - if the columns parameter is null.
        IllegalArgumentException - if a column is present two times in the list, or if the column is not owned by this Grid, or if the list doesn't contain all columns currently present in the Grid, or if the column rearranging would require to split a joined header/footer cell group.
        See Also:
        setColumnOrder(Column[])
      • scrollToIndex

        public void scrollToIndex​(int rowIndex)
        Scrolls to the given row index. Scrolls so that the row is shown at the start of the visible area whenever possible. If the index parameter exceeds current item set size the grid will scroll to the end.
        Parameters:
        rowIndex - zero based index of the item to scroll to in the current view.
      • scrollToStart

        public void scrollToStart()
        Scrolls to the beginning of the first data row.
      • scrollToEnd

        public void scrollToEnd()
        Scrolls to the last data row of the grid.
      • setNestedNullBehavior

        public void setNestedNullBehavior​(Grid.NestedNullBehavior nestedNullBehavior)
        Set the behavior when facing nested null values. By default the value is NestedNullBehavior.THROW.
        Parameters:
        nestedNullBehavior - the behavior when facing nested null values.
      • getNestedNullBehavior

        public Grid.NestedNullBehavior getNestedNullBehavior()
        Get the behavior when facing nested null values.
        Returns:
        The current behavior when facing nested null values.