com.vaadin.flow.component.grid.

Class Grid<T>

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

        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

    • Method Detail

      • addColumn

        public Grid.Column<T> addColumn(Renderer<T> renderer,
                                        String... sortingProperties)

        Adds a new text column to this Grid with a template renderer and sorting properties. 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.

        Parameters:

        renderer - the renderer used to create the grid cell structure

        sortingProperties - the sorting properties to use for this column

        Returns:

        the created column

      • addColumn

        public Grid.Column<T> addColumn(String propertyName)

        Adds a new column for the given property name. 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).

        Parameters:

        propertyName - the property name of the new column, not null

        Returns:

        the created 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

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

      • 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

      • 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

      • setDataProvider

        public void setDataProvider(DataProvider<T,?> dataProvider)

        Description copied from interface: HasDataProvider

        Sets the data provider for this listing. The data provider is queried for displayed items as needed.

        Specified by:

        setDataProvider in interface HasDataProvider<T>

        Parameters:

        dataProvider - the data provider, not null

      • getDataProvider

        public DataProvider<T,?> getDataProvider()

        Returns the data provider of this grid.

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

        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.

        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)

      • 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

        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

        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(value="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

      • removeColumn

        public void removeColumn(Grid.Column<T> column)

        Removes a column from the Grid.

        Parameters:

        column - the column to be removed, not null

        Throws:

        IllegalArgumentException - if column is null or if it is not part of 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)

      • createSortingComparator

        protected SerializableComparator<T> createSortingComparator()

        Creates a comparator for grid to sort rows.

        Returns:

        the comparator based on column sorting information.

      • setHeightByRows

        public void setHeightByRows(boolean heightByRows)

        If true, the grid's height is defined by the number of its rows. All items are fetched from the DataProvider, and the Grid shows no vertical scroll bar.

        Parameters:

        heightByRows - true to make Grid compute its height by the number of rows, false for the default behavior

      • isHeightByRows

        @Synchronize(value="height-by-rows-changed")
        public boolean isHeightByRows()

        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