Web Components Tree Grid Overview and Configuration

    Ignite UI for Web Components TreeTitle is used to display and manipulate hierarchical or flat data with ease. Quickly bind your data with very little code or use a variety of events to customize different behaviors. This component provides a rich set of features like data selection, excel style filtering, sorting, paging, templating and column moving. Displaying of tabular data has never been easier and beautiful thanks to the Material Table-based UI Tree Grid.

    Web Components Tree Grid Example

    In this example, you can see how users can manipulate hierarchical or flat data. We have included filtering and sorting options, pinning and hiding, row selection, export to excel and csv.

    Getting Started with Ignite UI for Web Components Tree Grid

    Dependencies

    To get started with the Web Components tree grid, first you need to install the Ignite UI for Web Components package.

    When installing the Web Components tree grid package, the core package must also be installed.

    npm install --save igniteui-webcomponents-core
    npm install --save igniteui-webcomponents-grids
    npm install --save igniteui-webcomponents-inputs
    

    You also need to include the following import to use the tree grid:

    import 'igniteui-webcomponents-grids/grids/combined.js';
    

    The corresponding styles should also be referenced. You can choose light or dark option for one of the themes and based on your project configuration to import it:

    import 'igniteui-webcomponents-grids/grids/themes/light/bootstrap.css';
    

    Or to link it:

    <link rel='stylesheet' href='node_modules/igniteui-webcomponents-grids/grids/themes/light/bootstrap.css'>
    

    For more details on how to customize the appearance of the tree grid, you may have a look at the styling section.

    Usage

    The tree grid shares a lot of features with the grid, but it also adds the ability to display its data hierarchically. In order to achieve this, the tree grid provides us with a couple of ways to define the relations among our data objects - by using a child collection for every data object or by using primary and foreign keys for every data object.

    <igc-tree-grid auto-generate="false" id="treeGrid" name="treeGrid">
        <igc-column field="name" header="Name" data-type="String"></igc-column>
        <igc-column field="hireDate" header="Hire Date" data-type="Date"></igc-column>
        <igc-column field="age" header="Age" data-type="Number"></igc-column>
    </igc-tree-grid>
    
        private _bind: () => void;
    
        constructor() {
            var treeGrid = (this.treeGrid = document.getElementById('treeGrid') as any) as IgcTreeGridComponent;
            this._bind = () => {
                treeGrid.childDataKey = "Employees";
                treeGrid.data = this.employeesNestedData;
            }
            this._bind();
        }
    

    Tree Cells

    Regardless of which option is used for building the tree grid's hierarchy (child collection or primary and foreign keys), the tree grid's rows are constructed of two types of cells:

    • GridCell - Ordinary cell that contains a value.
    • TreeGridCell - Tree cell that contains a value, an expand/collapse indicator and an indentation div element, which is based on the level of the cell's row. The level of a row component can be accessed through the level property of its inner treeRow.
    Note

    Each row can have only one tree cell, but it can have multiple (or none) ordinary cells.

    Initial Expansion Depth

    Initially the tree grid will expand all node levels and show them. This behavior can be configured using the expansionDepth property. By default its value is Infinity which means all node levels will be expanded. You may control the initial expansion depth by setting this property to a numeric value. For example 0 will show only root level nodes, 1 will show root level nodes and their child nodes and so on.

    Child Collection

    When we are using the child collection option, every data object contains a child collection, that is populated with items of the same type as the parent data object. This way every record in our tree grid will have a direct reference to any of its children. In this case the data property of our tree grid that contains the original data source will be a hierarchically defined collection.

    const EMPLOYEE_DATA = [
        {
            Name: "Johnathan Winchester",
            ID: 1,
            HireDate: new Date(2008, 3, 20),
            Age: 55,
            Employees: [
                {
                    Name: "Michael Burke",
                    ID: 3,
                    HireDate: new Date(2011, 6, 3),
                    Age: 43,
                    Employees: []
                },
                {
                    Name: "Thomas Anderson"
                    ID: 2,
                    HireDate: new Date(2009, 6, 19),
                    Age: 29,
                    Employees: []
                },
                ...
            ]
        },
        ...
    ]
    

    In order for the tree grid to build the hierarchy, we will have to set its childDataKey property to the name of the child collection that is used in each of our data objects. In our case that will be the Employees collection. In addition, we can disable the automatic column generation and define them manually by matching them to the actual properties of our data objects. (The Employees collection will be automatically used for the hierarchy, so there is no need to include it in the columns' definitions.)

    We can now enable the row selection and paging features of the tree grid by using the rowSelection and the paging properties. We can also enable the summaries, the filtering, sorting, editing, moving and resizing features for each of our columns.

    Finally, we can enable the toolbar of our tree grid, along with the column hiding, column pinning and exporting features by using the IgcGridToolbarComponent, IgcGridToolbarHiding, IgcGridToolbarPinning and IgcGridToolbarExporter respectively.

    Primary and Foreign keys

    When we are using the primary and foreign keys option, every data object contains a primary key and a foreign key. The primary key is the unique identifier of the current data object and the foreign key is the unique identifier of its parent. In this case the data property of our tree grid that contains the original data source will be a flat collection.

    const data = [
        { ID: 1, ParentID: -1, Name: "Casey Houston", JobTitle: "Vice President", Age: 32 },
        { ID: 2, ParentID: 1, Name: "Gilberto Todd", JobTitle: "Director", Age: 41 },
        { ID: 3, ParentID: 2, Name: "Tanya Bennett", JobTitle: "Director", Age: 29 },
        { ID: 4, ParentID: 2, Name: "Jack Simon", JobTitle: "Software Developer", Age: 33 },
        { ID: 5, ParentID: 8, Name: "Celia Martinez", JobTitle: "Senior Software Developer", Age: 44 },
        { ID: 6, ParentID: -1, Name: "Erma Walsh", JobTitle: "CEO", Age: 52 },
        { ID: 7, ParentID: 2, Name: "Debra Morton", JobTitle: "Associate Software Developer", Age: 35 },
        { ID: 8, ParentID: 10, Name: "Erika Wells", JobTitle: "Software Development Team Lead", Age: 50 },
        { ID: 9, ParentID: 8, Name: "Leslie Hansen", JobTitle: "Associate Software Developer", Age: 28 },
        { ID: 10, ParentID: -1, Name: "Eduardo Ramirez", JobTitle: "Development Manager", Age: 53 }
    ];
    

    In the sample data above, all records have an ID, a ParentID and some additional properties like Name, JobTitle and Age. As mentioned previously, the ID of the records must be unique as it will be our primaryKey. The ParentID contains the ID of the parent node and could be set as a foreignKey. If a row has a ParentID that does not match any row in the tree grid, then that means this row is a root row.

    Persistence and Integration

    The indentation of the tree cells persists across other tree grid features like filtering, sorting and paging.

    • When sorting is applied on a column, the data rows get sorted by levels. This means that the root level rows will be sorted independently from their respective children. Their respective children collections will each be sorted independently as well and so on.
    • The first column (the one that has a visibleIndex of 0) is always the tree column.
    • The column that ends up with a visibleIndex of 0 after operations like column pinning, column hiding and column moving becomes the tree column.
    • Exported Excel worksheets reflect the hierarchy by grouping the records as they are grouped in the tree grid itself. All records expanded states would also be persisted and reflected.
    • When exporting to CSV, levels and expanded states are ignored and all data is exported as flat.

    Web Components Tree Grid Styling Configuration

    In addition to the predefined themes, the tree grid could be further customized by setting some of the available CSS properties. In case you would like to change the header background and text color you need to set a class for the tree grid first:

    <igc-tree-grid class="tree-grid">
    

    Then set the --header-background and --header-text-color CSS properties for that class:

    .tree-grid {
        --header-background: #494949;
        --header-text-color: #FFF;
    }
    

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.