React Tree Grid Overview and Configuration
Ignite UI for React Tree Grid 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.
React 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.
Example
export class EmployeesNestedDataItem {
public constructor (init: Partial<EmployeesNestedDataItem> ) {
Object .assign(this , init);
}
public ID: number ;
public Age: number ;
public Salary: number ;
public Productivity: number ;
public City: string ;
public Country: string ;
public Phone: string ;
public HireDate: string ;
public Name: string ;
public Title: string ;
public Employees: EmployeesNestedDataItem_EmployeesItem[];
}
export class EmployeesNestedDataItem_EmployeesItem {
public constructor (init: Partial<EmployeesNestedDataItem_EmployeesItem> ) {
Object .assign(this , init);
}
public Age: number ;
public Salary: number ;
public Productivity: number ;
public City: string ;
public Country: string ;
public Phone: string ;
public HireDate: string ;
public ID: number ;
public Name: string ;
public Title: string ;
}
export class EmployeesNestedData extends Array <EmployeesNestedDataItem > {
public constructor (items: Array <EmployeesNestedDataItem> | number = -1 ) {
if (Array .isArray(items)) {
super (...items);
} else {
const newItems = [
new EmployeesNestedDataItem(
{
ID : 1 ,
Age : 55 ,
Salary : 80000 ,
Productivity : 90 ,
City : `Berlin` ,
Country : `Germany` ,
Phone : `609-202-505` ,
HireDate : `2008-03-20` ,
Name : `John Winchester` ,
Title : `Development Manager` ,
Employees : [
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 43 ,
Salary : 70000 ,
Productivity : 80 ,
City : `Hamburg` ,
Country : `Germany` ,
Phone : `609-444-555` ,
HireDate : `2011-06-03` ,
ID : 3 ,
Name : `Michael Burke` ,
Title : `Senior Software Developer`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 29 ,
Salary : 60000 ,
Productivity : 80 ,
City : `Munich` ,
Country : `Germany` ,
Phone : `609-333-444` ,
HireDate : `2009-06-19` ,
ID : 2 ,
Name : `Thomas Anderson` ,
Title : `Senior Software Developer`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 31 ,
Salary : 90000 ,
Productivity : 80 ,
City : `Warasw` ,
Country : `Poland` ,
Phone : `609-222-205` ,
HireDate : `2014-08-18` ,
ID : 11 ,
Name : `Monica Reyes` ,
Title : `Software Development Team Lead`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 35 ,
Salary : 70000 ,
Productivity : 70 ,
City : `Koln` ,
Country : `Germany` ,
Phone : `609-502-525` ,
HireDate : `2015-09-17` ,
ID : 6 ,
Name : `Roland Mendel` ,
Title : `Senior Software Developer`
})]
}),
new EmployeesNestedDataItem(
{
ID : 4 ,
Age : 42 ,
Salary : 90000 ,
Productivity : 80 ,
City : `Kielce` ,
Country : `Poland` ,
Phone : `609-202-505` ,
HireDate : `2014-01-22` ,
Name : `Ana Sanders` ,
Title : `CEO` ,
Employees : [
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 44 ,
Salary : 80000 ,
Productivity : 80 ,
City : `Warasw` ,
Country : `Poland` ,
Phone : `609-202-505` ,
HireDate : `2014-04-04` ,
ID : 14 ,
Name : `Laurence Johnson` ,
Title : `Director`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 25 ,
Salary : 85000 ,
Productivity : 55 ,
City : `Paris` ,
Country : `France` ,
Phone : `609-202-505` ,
HireDate : `2017-11-09` ,
ID : 5 ,
Name : `Elizabeth Richards` ,
Title : `Vice President`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 39 ,
Salary : 88000 ,
Productivity : 88 ,
City : `London` ,
Country : `UK` ,
Phone : `609-202-505` ,
HireDate : `2010-03-22` ,
ID : 13 ,
Name : `Trevor Ashworth` ,
Title : `Director`
})]
}),
new EmployeesNestedDataItem(
{
ID : 18 ,
Age : 49 ,
Salary : 77000 ,
Productivity : 70 ,
City : `Manchester` ,
Country : `UK` ,
Phone : `222-555-577` ,
HireDate : `2014-01-22` ,
Name : `Victoria Lincoln` ,
Title : `Senior Accountant` ,
Employees : [
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 43 ,
Salary : 70000 ,
Productivity : 80 ,
City : `Hamburg` ,
Country : `Germany` ,
Phone : `609-444-555` ,
HireDate : `2011-06-03` ,
ID : 23 ,
Name : `Thomas Burke` ,
Title : `Senior Accountant`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 29 ,
Salary : 60000 ,
Productivity : 80 ,
City : `Munich` ,
Country : `Germany` ,
Phone : `609-333-444` ,
HireDate : `2009-06-19` ,
ID : 22 ,
Name : `Michael Anderson` ,
Title : `Junior Accountant`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 31 ,
Salary : 90000 ,
Productivity : 80 ,
City : `Warasw` ,
Country : `Poland` ,
Phone : `609-222-205` ,
HireDate : `2014-08-18` ,
ID : 21 ,
Name : `Roland Reyes` ,
Title : `Accountant Team Lead`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 35 ,
Salary : 70000 ,
Productivity : 70 ,
City : `Koln` ,
Country : `Germany` ,
Phone : `609-502-525` ,
HireDate : `2015-09-17` ,
ID : 24 ,
Name : `Monica Mendel` ,
Title : `Senior Software Developer`
})]
}),
new EmployeesNestedDataItem(
{
ID : 10 ,
Age : 61 ,
Salary : 85000 ,
Productivity : 890 ,
City : `Lyon` ,
Country : `France` ,
Phone : `259-266-887` ,
HireDate : `2010-01-01` ,
Name : `Yang Wang` ,
Title : `Localization Developer` ,
Employees : [
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 31 ,
Salary : 90000 ,
Productivity : 80 ,
City : `Warasw` ,
Country : `Poland` ,
Phone : `609-222-205` ,
HireDate : `2014-08-18` ,
ID : 11 ,
Name : `Monica Reyes` ,
Title : `Software Development Team Lead`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 35 ,
Salary : 70000 ,
Productivity : 70 ,
City : `Koln` ,
Country : `Germany` ,
Phone : `609-502-525` ,
HireDate : `2015-09-17` ,
ID : 6 ,
Name : `Roland Mendel` ,
Title : `Senior Software Developer`
})]
}),
new EmployeesNestedDataItem(
{
ID : 35 ,
Age : 35 ,
Salary : 75000 ,
Productivity : 75 ,
City : `Warasw` ,
Country : `Poland` ,
Phone : `688-244-844` ,
HireDate : `2014-01-22` ,
Name : `Janine Munoz` ,
Title : `HR` ,
Employees : [
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 43 ,
Salary : 70000 ,
Productivity : 80 ,
City : `Hamburg` ,
Country : `Germany` ,
Phone : `609-444-555` ,
HireDate : `2011-06-03` ,
ID : 3 ,
Name : `Michael Burke` ,
Title : `Senior Software Developer`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 31 ,
Salary : 90000 ,
Productivity : 80 ,
City : `Warasw` ,
Country : `Poland` ,
Phone : `609-222-205` ,
HireDate : `2014-08-18` ,
ID : 11 ,
Name : `Monica Reyes` ,
Title : `Software Development Team Lead`
})]
}),
new EmployeesNestedDataItem(
{
ID : 10 ,
Age : 49 ,
Salary : 95000 ,
Productivity : 80 ,
City : `Krakow` ,
Country : `Poland` ,
Phone : `677-266-555` ,
HireDate : `2010-01-01` ,
Name : `Yang Wang` ,
Title : `Sales Manager` ,
Employees : [
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 29 ,
Salary : 60000 ,
Productivity : 80 ,
City : `Munich` ,
Country : `Germany` ,
Phone : `609-333-444` ,
HireDate : `2009-06-19` ,
ID : 2 ,
Name : `Thomas Anderson` ,
Title : `Senior Software Developer`
}),
new EmployeesNestedDataItem_EmployeesItem(
{
Age : 35 ,
Salary : 70000 ,
Productivity : 70 ,
City : `Koln` ,
Country : `Germany` ,
Phone : `609-502-525` ,
HireDate : `2015-09-17` ,
ID : 6 ,
Name : `Roland Mendel` ,
Title : `Senior Software Developer`
})]
}),
];
super (...newItems.slice(0 ));
}
}
}
ts コピー import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { IgrPropertyEditorPanelModule } from "@infragistics/igniteui-react-layouts" ;
import { IgrTreeGridModule, IgrPaginatorModule } from "@infragistics/igniteui-react-grids" ;
import { IgrTreeGrid, IgrPaginator, IgrGridToolbar, IgrGridToolbarTitle, IgrGridToolbarActions, IgrGridToolbarHiding, IgrGridToolbarPinning, IgrGridToolbarExporter, IgrColumn } from "@infragistics/igniteui-react-grids" ;
import { ComponentRenderer, PropertyEditorPanelDescriptionModule, WebTreeGridDescriptionModule, WebPaginatorDescriptionModule } from "@infragistics/igniteui-react-core" ;
import { EmployeesNestedDataItem, EmployeesNestedDataItem_EmployeesItem, EmployeesNestedData } from './EmployeesNestedData' ;
import "@infragistics/igniteui-react-grids/grids/combined" ;
import "@infragistics/igniteui-react-grids/grids/themes/light/bootstrap.css" ;
const mods : any [] = [
IgrPropertyEditorPanelModule,
IgrTreeGridModule,
IgrPaginatorModule
];
mods.forEach((m) => m.register());
export default class Sample extends React.Component <any, any> {
private treeGrid: IgrTreeGrid
private treeGridRef(r: IgrTreeGrid) {
this .treeGrid = r;
this .setState({});
}
private employees: IgrGridToolbarTitle
constructor (props: any ) {
super (props);
this .treeGridRef = this .treeGridRef.bind(this );
}
public render (): JSX .Element {
return (
<div className ="container sample ig-typography" >
<div className ="container fill" >
<IgrTreeGrid
autoGenerate ="false"
ref ={this.treeGridRef}
id ="treeGrid"
data ={this.employeesNestedData}
childDataKey ="Employees"
rowSelection ="Multiple"
moving ="true"
allowFiltering ="true"
filterMode ="ExcelStyleFilter" >
<IgrPaginator
>
</IgrPaginator >
<IgrGridToolbar
>
<IgrGridToolbarTitle
name ="Employees" >
</IgrGridToolbarTitle >
<IgrGridToolbarActions
>
<IgrGridToolbarHiding
>
</IgrGridToolbarHiding >
<IgrGridToolbarPinning
>
</IgrGridToolbarPinning >
<IgrGridToolbarExporter
exportCSV ="true"
exportExcel ="true" >
</IgrGridToolbarExporter >
</IgrGridToolbarActions >
</IgrGridToolbar >
<IgrColumn
field ="Name"
header ="Name"
dataType ="String"
sortable ="true"
editable ="true"
resizable ="true"
hasSummary ="true" >
</IgrColumn >
<IgrColumn
field ="HireDate"
header ="Hire Date"
dataType ="Date"
sortable ="true"
editable ="true"
resizable ="true" >
</IgrColumn >
<IgrColumn
field ="Age"
header ="Age"
dataType ="Number"
sortable ="true"
editable ="true"
resizable ="true" >
</IgrColumn >
</IgrTreeGrid >
</div >
</div >
);
}
private _employeesNestedData: EmployeesNestedData = null ;
public get employeesNestedData(): EmployeesNestedData {
if (this ._employeesNestedData == null )
{
this ._employeesNestedData = new EmployeesNestedData();
}
return this ._employeesNestedData;
}
private _componentRenderer: ComponentRenderer = null ;
public get renderer(): ComponentRenderer {
if (this ._componentRenderer == null ) {
this ._componentRenderer = new ComponentRenderer();
var context = this ._componentRenderer.context;
PropertyEditorPanelDescriptionModule.register(context);
WebTreeGridDescriptionModule.register(context);
WebPaginatorDescriptionModule.register(context);
}
return this ._componentRenderer;
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<Sample /> );
tsx コピー
Like this sample? Get access to our complete Ignite UI for React toolkit and start building your own apps in minutes. Download it for free.
Getting Started with Ignite UI for React Tree Grid
Dependencies
To get started with the React tree grid, first you need to install the igniteui-react
and igniteui-react-grids
packages.
npm install --save igniteui-react
npm install --save igniteui-react-grids
cmd
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.
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
.
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 the 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.
For this sample, let's use the following collection structure:
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 : []
},
]
},
]
typescript
Now let's start by importing our Data
collection and binding it to our tree grid.
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 IgrGridToolbar
, IgrGridToolbarHiding
, IgrGridToolbarPinning
and IgrGridToolbarExporter
respectively.
You can see the result of the code from above at the beginning of this article in the Tree Grid Example section.
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 }
];
typescript
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.
The parent-child relation is configured using the tree grid's PrimaryKey
and ForeignKey
properties.
Here is the template of the component which demonstrates how to configure the tree grid to display the data defined in the above flat collection:
In addition we will enable the row selection feature of the tree grid by using the rowSelection property and also the filtering, sorting, editing, moving and resizing features for each of our columns.
And here is the final result:
export class EmployeesFlatDataItem {
public constructor (init: Partial<EmployeesFlatDataItem> ) {
Object .assign(this , init);
}
public Age: number ;
public HireDate: string ;
public ID: number ;
public Name: string ;
public Phone: string ;
public OnPTO: boolean ;
public ParentID: number ;
public Title: string ;
}
export class EmployeesFlatData extends Array <EmployeesFlatDataItem > {
public constructor (items: Array <EmployeesFlatDataItem> | number = -1 ) {
if (Array .isArray(items)) {
super (...items);
} else {
const newItems = [
new EmployeesFlatDataItem(
{
Age : 55 ,
HireDate : `2008-03-20` ,
ID : 1 ,
Name : `Johnathan Winchester` ,
Phone : `0251-031259` ,
OnPTO : false ,
ParentID : -1 ,
Title : `Development Manager`
}),
new EmployeesFlatDataItem(
{
Age : 42 ,
HireDate : `2014-01-22` ,
ID : 4 ,
Name : `Ana Sanders` ,
Phone : `(21) 555-0091` ,
OnPTO : true ,
ParentID : -1 ,
Title : `CEO`
}),
new EmployeesFlatDataItem(
{
Age : 49 ,
HireDate : `2014-01-22` ,
ID : 18 ,
Name : `Victoria Lincoln` ,
Phone : `(071) 23 67 22 20` ,
OnPTO : true ,
ParentID : -1 ,
Title : `Accounting Manager`
}),
new EmployeesFlatDataItem(
{
Age : 61 ,
HireDate : `2010-01-01` ,
ID : 10 ,
Name : `Yang Wang` ,
Phone : `(21) 555-0091` ,
OnPTO : false ,
ParentID : -1 ,
Title : `Localization Manager`
}),
new EmployeesFlatDataItem(
{
Age : 43 ,
HireDate : `2011-06-03` ,
ID : 3 ,
Name : `Michael Burke` ,
Phone : `0452-076545` ,
OnPTO : true ,
ParentID : 1 ,
Title : `Senior Software Developer`
}),
new EmployeesFlatDataItem(
{
Age : 29 ,
HireDate : `2009-06-19` ,
ID : 2 ,
Name : `Thomas Anderson` ,
Phone : `(14) 555-8122` ,
OnPTO : false ,
ParentID : 1 ,
Title : `Senior Software Developer`
}),
new EmployeesFlatDataItem(
{
Age : 31 ,
HireDate : `2014-08-18` ,
ID : 11 ,
Name : `Monica Reyes` ,
Phone : `7675-3425` ,
OnPTO : false ,
ParentID : 1 ,
Title : `Software Development Team Lead`
}),
new EmployeesFlatDataItem(
{
Age : 35 ,
HireDate : `2015-09-17` ,
ID : 6 ,
Name : `Roland Mendel` ,
Phone : `(505) 555-5939` ,
OnPTO : false ,
ParentID : 11 ,
Title : `Senior Software Developer`
}),
new EmployeesFlatDataItem(
{
Age : 44 ,
HireDate : `2009-10-11` ,
ID : 12 ,
Name : `Sven Cooper` ,
Phone : `0695-34 67 21` ,
OnPTO : true ,
ParentID : 11 ,
Title : `Senior Software Developer`
}),
new EmployeesFlatDataItem(
{
Age : 44 ,
HireDate : `2014-04-04` ,
ID : 14 ,
Name : `Laurence Johnson` ,
Phone : `981-443655` ,
OnPTO : false ,
ParentID : 4 ,
Title : `Director`
}),
new EmployeesFlatDataItem(
{
Age : 25 ,
HireDate : `2017-11-09` ,
ID : 5 ,
Name : `Elizabeth Richards` ,
Phone : `(2) 283-2951` ,
OnPTO : true ,
ParentID : 4 ,
Title : `Vice President`
}),
new EmployeesFlatDataItem(
{
Age : 39 ,
HireDate : `2010-03-22` ,
ID : 13 ,
Name : `Trevor Ashworth` ,
Phone : `981-443655` ,
OnPTO : true ,
ParentID : 5 ,
Title : `Director`
}),
new EmployeesFlatDataItem(
{
Age : 44 ,
HireDate : `2014-04-04` ,
ID : 17 ,
Name : `Antonio Moreno` ,
Phone : `(505) 555-5939` ,
OnPTO : false ,
ParentID : 18 ,
Title : `Senior Accountant`
}),
new EmployeesFlatDataItem(
{
Age : 50 ,
HireDate : `2007-11-18` ,
ID : 7 ,
Name : `Pedro Rodriguez` ,
Phone : `035-640230` ,
OnPTO : false ,
ParentID : 10 ,
Title : `Senior Localization Developer`
}),
new EmployeesFlatDataItem(
{
Age : 27 ,
HireDate : `2016-02-19` ,
ID : 8 ,
Name : `Casey Harper` ,
Phone : `0342-023176` ,
OnPTO : true ,
ParentID : 10 ,
Title : `Senior Localization`
}),
new EmployeesFlatDataItem(
{
Age : 25 ,
HireDate : `2017-11-09` ,
ID : 15 ,
Name : `Patricia Simpson` ,
Phone : `069-0245984` ,
OnPTO : false ,
ParentID : 7 ,
Title : `Localization Intern`
}),
new EmployeesFlatDataItem(
{
Age : 39 ,
HireDate : `2010-03-22` ,
ID : 9 ,
Name : `Francisco Chang` ,
Phone : `(91) 745 6200` ,
OnPTO : false ,
ParentID : 7 ,
Title : `Localization Intern`
}),
new EmployeesFlatDataItem(
{
Age : 25 ,
HireDate : `2018-03-18` ,
ID : 16 ,
Name : `Peter Lewis` ,
Phone : `069-0245984` ,
OnPTO : true ,
ParentID : 7 ,
Title : `Localization Intern`
}),
];
super (...newItems.slice(0 ));
}
}
}
ts コピー import React from 'react' ;
import ReactDOM from 'react-dom/client' ;
import './index.css' ;
import { IgrTreeGridModule } from "@infragistics/igniteui-react-grids" ;
import { IgrTreeGrid, IgrColumn } from "@infragistics/igniteui-react-grids" ;
import { EmployeesFlatDataItem, EmployeesFlatData } from './EmployeesFlatData' ;
import "@infragistics/igniteui-react-grids/grids/combined" ;
import "@infragistics/igniteui-react-grids/grids/themes/light/bootstrap.css" ;
const mods : any [] = [
IgrTreeGridModule
];
mods.forEach((m) => m.register());
export default class Sample extends React.Component <any, any> {
private treeGrid: IgrTreeGrid
private treeGridRef(r: IgrTreeGrid) {
this .treeGrid = r;
this .setState({});
}
constructor (props: any ) {
super (props);
this .treeGridRef = this .treeGridRef.bind(this );
}
public render (): JSX .Element {
return (
<div className ="container sample ig-typography" >
<div className ="container fill" >
<IgrTreeGrid
autoGenerate ="false"
ref ={this.treeGridRef}
id ="treeGrid"
data ={this.employeesFlatData}
primaryKey ="ID"
foreignKey ="ParentID"
allowFiltering ="true"
moving ="true"
rowSelection ="Multiple" >
<IgrColumn
field ="Name"
dataType ="String"
sortable ="true"
editable ="true"
resizable ="true" >
</IgrColumn >
<IgrColumn
field ="Title"
header ="Job Title"
dataType ="String"
sortable ="true"
editable ="true"
resizable ="true" >
</IgrColumn >
<IgrColumn
field ="Age"
dataType ="Number"
sortable ="true"
editable ="true"
resizable ="true" >
</IgrColumn >
</IgrTreeGrid >
</div >
</div >
);
}
private _employeesFlatData: EmployeesFlatData = null ;
public get employeesFlatData(): EmployeesFlatData {
if (this ._employeesFlatData == null )
{
this ._employeesFlatData = new EmployeesFlatData();
}
return this ._employeesFlatData;
}
}
const root = ReactDOM.createRoot (document.getElementById('root' ));
root.render (<Sample /> );
tsx コピー
Persistence and Integration
The indentation of the tree grid cell 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.
API References
Additional Resources
Our community is active and always welcoming to new ideas.