Class TreeGrid<T>

  • Type Parameters:
    T - the grid bean type
    All Implemented Interfaces:
    com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.BlurNotifier<Grid<T>>, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.Focusable<Grid<T>>, com.vaadin.flow.component.FocusNotifier<Grid<T>>, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasTheme, com.vaadin.flow.data.event.SortEvent.SortNotifier<Grid<T>,​GridSortOrder<T>>, com.vaadin.flow.data.provider.HasDataGenerators<T>, com.vaadin.flow.data.provider.HasDataView<T,​Void,​GridDataView<T>>, com.vaadin.flow.data.provider.HasLazyDataView<T,​Void,​GridLazyDataView<T>>, com.vaadin.flow.data.provider.HasListDataView<T,​GridListDataView<T>>, com.vaadin.flow.data.provider.hierarchy.HasHierarchicalDataProvider<T>, Serializable

    @JsModule("@vaadin/grid/src/vaadin-grid-tree-toggle.js")
    public class TreeGrid<T>
    extends Grid<T>
    implements com.vaadin.flow.data.provider.hierarchy.HasHierarchicalDataProvider<T>
    Tree Grid is a component for displaying hierarchical tabular data grouped into expandable and collapsible nodes. Tree Grid is an extension of the Grid component and all Grid’s features are available in Tree Grid as well.
    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Constructor Detail

      • TreeGrid

        public TreeGrid()
        Creates a new TreeGrid without support for creating columns based on property names. Use an alternative constructor, such as TreeGrid(Class), to create a TreeGrid that automatically sets up columns based on the type of presented data.
      • TreeGrid

        public TreeGrid​(Class<T> beanType)
        Creates a new TreeGrid 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.
        Parameters:
        beanType - the bean type to use, not null
      • TreeGrid

        public TreeGrid​(com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider<T,​?> dataProvider)
        Creates a new TreeGrid using the given HierarchicalDataProvider, without support for creating columns based on property names. Use an alternative constructor, such as TreeGrid(Class), to create a TreeGrid that automatically sets up columns based on the type of presented data.
        Parameters:
        dataProvider - the data provider, not null
    • Method Detail

      • setUniqueKeyDataGenerator

        public void setUniqueKeyDataGenerator​(String propertyName,
                                              com.vaadin.flow.function.ValueProvider<T,​String> uniqueKeyProvider)
        Sets property name and value provider for unique key in row's generated JSON.

        Default property name is 'key' and value is generated by bean's hashCode method.

        Parameters:
        propertyName - Property name in JSON data
        uniqueKeyProvider - Value provider for the target property in JSON data
      • addExpandListener

        public com.vaadin.flow.shared.Registration addExpandListener​(com.vaadin.flow.component.ComponentEventListener<ExpandEvent<T,​TreeGrid<T>>> listener)
        Adds an ExpandEvent listener to this TreeGrid.
        Parameters:
        listener - the listener to add
        Returns:
        a registration for the listener
        See Also:
        ExpandEvent
      • addCollapseListener

        public com.vaadin.flow.shared.Registration addCollapseListener​(com.vaadin.flow.component.ComponentEventListener<CollapseEvent<T,​TreeGrid<T>>> listener)
        Adds a CollapseEvent listener to this TreeGrid.
        Parameters:
        listener - the listener to add
        Returns:
        a registration for the listener
        See Also:
        CollapseEvent
      • setDataProvider

        public void setDataProvider​(com.vaadin.flow.data.provider.DataProvider<T,​?> dataProvider)
        Description copied from class: Grid
        Specified by:
        setDataProvider in interface com.vaadin.flow.data.provider.hierarchy.HasHierarchicalDataProvider<T>
        Overrides:
        setDataProvider in class Grid<T>
      • setDataProvider

        public void setDataProvider​(com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider<T,​?> hierarchicalDataProvider)
        Specified by:
        setDataProvider in interface com.vaadin.flow.data.provider.hierarchy.HasHierarchicalDataProvider<T>
      • setItems

        @Deprecated
        public GridLazyDataView<T> setItems​(com.vaadin.flow.data.provider.CallbackDataProvider.FetchCallback<T,​Void> fetchCallback)
        Deprecated.
        use setDataProvider(HierarchicalDataProvider), HasHierarchicalDataProvider.setItems(Collection, ValueProvider), HasHierarchicalDataProvider.setItems(Stream, ValueProvider) or HasHierarchicalDataProvider.setTreeData(TreeData) instead.
        Tree grid supports only hierarchical data so use another method instead. This method is inherited from Grid and it will throw an UnsupportedOperationException.
        Specified by:
        setItems in interface com.vaadin.flow.data.provider.HasLazyDataView<T,​Void,​GridLazyDataView<T>>
        Parameters:
        fetchCallback - the fetch callback
        Returns:
        the data view
      • setItems

        @Deprecated
        public GridListDataView<T> setItems​(com.vaadin.flow.data.provider.ListDataProvider<T> dataProvider)
        Deprecated.
        use setDataProvider(HierarchicalDataProvider), HasHierarchicalDataProvider.setItems(Collection, ValueProvider), HasHierarchicalDataProvider.setItems(Stream, ValueProvider) or HasHierarchicalDataProvider.setTreeData(TreeData) instead.
        Tree grid supports only hierarchical data providers so use another method instead. This method is inherited from Grid and it will throw an UnsupportedOperationException.
        Specified by:
        setItems in interface com.vaadin.flow.data.provider.HasListDataView<T,​GridListDataView<T>>
        Overrides:
        setItems in class Grid<T>
        Parameters:
        dataProvider - the data provider
        Returns:
        the data view
      • setItems

        @Deprecated
        public GridListDataView<T> setItems​(T... items)
        Deprecated.
        use setDataProvider(HierarchicalDataProvider), HasHierarchicalDataProvider.setItems(Collection, ValueProvider), HasHierarchicalDataProvider.setItems(Stream, ValueProvider) or HasHierarchicalDataProvider.setTreeData(TreeData) instead.
        Tree grid supports only hierarchical data so use another method instead. This method is inherited from Grid and it will throw an UnsupportedOperationException.
        Specified by:
        setItems in interface com.vaadin.flow.data.provider.HasListDataView<T,​GridListDataView<T>>
        Parameters:
        items - the items to display, not null
        Returns:
        the data view
      • setItems

        @Deprecated
        public GridListDataView<T> setItems​(Collection<T> items)
        Deprecated.
        use setDataProvider(HierarchicalDataProvider), HasHierarchicalDataProvider.setItems(Collection, ValueProvider), HasHierarchicalDataProvider.setItems(Stream, ValueProvider) or HasHierarchicalDataProvider.setTreeData(TreeData) instead.
        Tree grid supports only hierarchical data, so use another method instead. This method is inherited from Grid and it will throw an UnsupportedOperationException.
        Specified by:
        setItems in interface com.vaadin.flow.data.provider.HasListDataView<T,​GridListDataView<T>>
        Parameters:
        items - the items to display, not null
        Returns:
        the data view
      • addHierarchyColumn

        public Grid.Column<T> addHierarchyColumn​(com.vaadin.flow.function.ValueProvider<T,​?> valueProvider)
        Adds a new Hierarchy column to this Grid with a value provider. The value is converted to String when sent to the client by using String.valueOf(Object).

        Hierarchy column is rendered by using 'vaadin-grid-tree-toggle' web component.

        Parameters:
        valueProvider - the value provider
        Returns:
        the created hierarchy column
      • addComponentHierarchyColumn

        public <V extends com.vaadin.flow.component.Component> Grid.Column<T> addComponentHierarchyColumn​(com.vaadin.flow.function.ValueProvider<T,​V> componentProvider)
        Adds a new Hierarchy column that shows components.

        NOTE: Using ComponentRenderer is not as efficient as the built in renderers.

        Type Parameters:
        V - the component type
        Parameters:
        componentProvider - a value provider that will return a component for the given item
        Returns:
        the new column
        See Also:
        Grid.addColumn(Renderer), Grid.removeColumn(Column)
      • setHierarchyColumn

        public Grid.Column<T> setHierarchyColumn​(String propertyName)
        Note: This method can only be used for a TreeGrid created from a bean type with TreeGrid(Class).

        Resets columns and their order based on bean properties.

        This is a shortcut for removing all columns and then calling Grid.addColumn(String) for each property except hierarchy column in the bean and addHierarchyColumn(String) for the given propertyName.

        Previous column order is preserved.

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

        Note that this also resets the headers and footers.

        Parameters:
        propertyName - a target hierarchy column property name
        Returns:
        the created hierarchy column
      • setHierarchyColumn

        public Grid.Column<T> setHierarchyColumn​(String propertyName,
                                                 com.vaadin.flow.function.ValueProvider<T,​?> valueProvider)
        Note: This method can only be used for a TreeGrid created from a bean type with TreeGrid(Class).

        Resets columns and their order based on bean properties.

        This is a shortcut for removing all columns and then calling Grid.addColumn(String) for each property except hierarchy column in the bean and addHierarchyColumn(String) or addHierarchyColumn(ValueProvider) for the given propertyName.

        Previous column order is preserved.

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

        Note that this also resets the headers and footers.

        Parameters:
        propertyName - a target hierarchy column property name
        valueProvider - optional value provider
        Returns:
        the created hierarchy column
      • setColumns

        public Grid.Column<T> setColumns​(String hierarchyPropertyName,
                                         com.vaadin.flow.function.ValueProvider<T,​?> valueProvider,
                                         Collection<String> propertyNames)
        Note: This method can only be used for a TreeGrid created from a bean type with TreeGrid(Class).

        Sets the columns and their order based on the given properties.

        This is a shortcut for removing all columns and then calling Grid.addColumn(String) for each property except hierarchy property in the bean and addHierarchyColumn(String) for the given hierarchyPropertyName.

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

        Note that this also resets the headers and footers.

        Parameters:
        hierarchyPropertyName - a target hierarchy column property name
        valueProvider - optional value provider
        propertyNames - set of properties to create columns for. Including given hierarchyPropertyName
        Returns:
        the hierarchy column
      • expand

        public void expand​(T... items)
        Expands the given items.

        If an item is currently expanded, does nothing. If an item does not have any children, does nothing.

        Parameters:
        items - the items to expand
      • expand

        public void expand​(Collection<T> items)
        Expands the given items.

        If an item is currently expanded, does nothing. If an item does not have any children, does nothing.

        Parameters:
        items - the items to expand
      • expand

        protected void expand​(Collection<T> items,
                              boolean userOriginated)
        Expands the given items.
        Parameters:
        items - the items to expand
        userOriginated - true if a ExpandEvent triggered by this operation is user originated, false otherwise.
      • expandRecursively

        public void expandRecursively​(Stream<T> items,
                                      int depth)
        Expands the given items and their children recursively until the given depth.

        depth describes the maximum distance between a given item and its descendant, meaning that expandRecursively(items, 0) expands only the given items while expandRecursively(items, 2) expands the given items as well as their children and grandchildren.

        This method will not fire events for expanded nodes.

        Parameters:
        items - the items to expand recursively
        depth - the maximum depth of recursion
        Since:
        8.4
      • expandRecursively

        public void expandRecursively​(Collection<T> items,
                                      int depth)
        Expands the given items and their children recursively until the given depth.

        depth describes the maximum distance between a given item and its descendant, meaning that expandRecursively(items, 0) expands only the given items while expandRecursively(items, 2) expands the given items as well as their children and grandchildren.

        This method will not fire events for expanded nodes.

        Parameters:
        items - the items to expand recursively
        depth - the maximum depth of recursion
        Since:
        8.4
      • collapse

        public void collapse​(T... items)
        Collapse the given items.

        For items that are already collapsed, does nothing.

        Parameters:
        items - the collection of items to collapse
      • collapse

        public void collapse​(Collection<T> items)
        Collapse the given items.

        For items that are already collapsed, does nothing.

        Parameters:
        items - the collection of items to collapse
      • collapse

        protected void collapse​(Collection<T> items,
                                boolean userOriginated)
        Collapse the given items.
        Parameters:
        items - the collection of items to collapse
        userOriginated - true if a CollapseEvent triggered by this operation is user originated, false otherwise.
      • collapseRecursively

        public void collapseRecursively​(Stream<T> items,
                                        int depth)
        Collapse the given items and their children recursively until the given depth.

        depth describes the maximum distance between a given item and its descendant, meaning that collapseRecursively(items, 0) collapses only the given items while collapseRecursively(items, 2) collapses the given items as well as their children and grandchildren.

        This method will not fire events for collapsed nodes.

        Parameters:
        items - the items to collapse recursively
        depth - the maximum depth of recursion
        Since:
        8.4
      • collapseRecursively

        public void collapseRecursively​(Collection<T> items,
                                        int depth)
        Collapse the given items and their children recursively until the given depth.

        depth describes the maximum distance between a given item and its descendant, meaning that collapseRecursively(items, 0) collapses only the given items while collapseRecursively(items, 2) collapses the given items as well as their children and grandchildren.

        This method will not fire events for collapsed nodes.

        Parameters:
        items - the items to collapse recursively
        depth - the maximum depth of recursion
        Since:
        8.4
      • getItemsWithChildrenRecursively

        protected Collection<T> getItemsWithChildrenRecursively​(Collection<T> items,
                                                                int depth)
        Gets given items and their children recursively until the given depth.

        depth describes the maximum distance between a given item and its descendant, meaning that getItemsWithChildrenRecursively(items, 0) gets only the given items while getItemsWithChildrenRecursively(items, 2) gets the given items as well as their children and grandchildren.

        Parameters:
        items - the items to expand recursively
        depth - the maximum depth of recursion
        Returns:
        collection of given items and their children recursively until the given depth
      • isExpanded

        public boolean isExpanded​(T item)
        Returns whether a given item is expanded or collapsed.
        Parameters:
        item - the item to check
        Returns:
        true if the item is expanded, false if collapsed
      • getDataCommunicator

        public com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicator<T> getDataCommunicator()
        Description copied from class: Grid
        Returns the data communicator of this Grid.
        Overrides:
        getDataCommunicator in class Grid<T>
        Returns:
        the data communicator, not null
      • getDataProvider

        public com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider<T,​com.vaadin.flow.function.SerializablePredicate<T>> getDataProvider()
        Description copied from class: Grid
        Returns the data provider of this grid.

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

        Specified by:
        getDataProvider in interface com.vaadin.flow.data.provider.hierarchy.HasHierarchicalDataProvider<T>
        Overrides:
        getDataProvider in class Grid<T>
        Returns:
        the data provider of this grid, not null
      • scrollToIndex

        public void scrollToIndex​(int rowIndex)
        The effective index of an item depends on the complete hierarchy of the tree. TreeGrid uses lazy loading for performance reasons and does not know about the complete hierarchy. Without the knowledge of the complete hierarchy, TreeGrid can’t reliably calculate an exact scroll position. This uncertainty makes this method unreliable and so should be avoided.
        Overrides:
        scrollToIndex in class Grid<T>
        Parameters:
        rowIndex - zero based index of the item to scroll to in the current view.