Columns in React Treegrid component
2 Jan 202424 minutes to read
The column definitions are used as the dataSource
schema in the TreeGrid. This plays a vital role in rendering column values in the required format. The treegrid operations such as sorting, filtering and searching etc. are performed based on column definitions. The field
property of the columns
is necessary to map the data source values in TreeGrid columns.
treeColumnIndex
property denotes the column that is used to expand and collapse child rows.
Format
To format cell values based on specific culture, use the columns.format
property. The TreeGrid uses Internalization library to format number
and date
values.
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='90' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='180'/>
<ColumnDirective field='unitPrice' headerText='Price Per Unit' width='90' format='c2' textAlign='Right' type='number'/>
</ColumnsDirective>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='90' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='180'/>
<ColumnDirective field='unitPrice' headerText='Price Per Unit' width='90' format='c2' textAlign='Right' type='number' />
</ColumnsDirective>
</TreeGridComponent>
};
export default App;
By default, the
number
anddate
values are formatted in en-US locale.
Number formatting
The number or integer values can be formatted using the below format strings.
Format | Description | Remarks |
---|---|---|
N | Denotes numeric type. | The numeric format is followed by integer value as N2, N3. etc which denotes the number of precision to be allowed. |
C | Denotes currency type. | The currency format is followed by integer value as C2, C3. etc which denotes the number of precision to be allowed. |
P | Denotes percentage type | The percentage format expects the input value to be in the range of 0 to 1. For example the cell value 0.2 is formatted as 20% . The percentage format is followed by integer value as P2, P3. etc which denotes the number of precision to be allowed. |
Please refer to the link to know more about Number formatting
.
Date formatting
You can format date values either using built-in date format string or custom format string.
For built-in date format you can specify columns.format
property as string (Example: yMd
). Please refer to the link to know more about Date formatting
.
You can also use custom format string to format the date values. Some of the custom formats and the formatted date values are given in the below table.
Format | Formatted value |
---|---|
{ type:’date’, format:’dd/MM/yyyy’ } | 04/07/1996 |
{ type:’date’, format:’dd.MM.yyyy’ } | 04.07.1996 |
{ type:’date’, skeleton:’short’ } | 7/4/96 |
{ type: ‘dateTime’, format: ‘dd/MM/yyyy hh:mm a’ } | 04/07/1996 12:00 AM |
{ type: ‘dateTime’, format: ‘MM/dd/yyyy hh:mm:ss a’ } | 07/04/1996 12:00:00 AM |
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
const formatOption = { type: 'date', format: 'dd/MM/yyyy' };
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='90' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='180'/>
<ColumnDirective field='orderDate' headerText='Order Date' width='160' textAlign='Right' format={formatOption}/>
<ColumnDirective field='price' headerText='Price' width='90' format='c2' textAlign='Right' type='number'/>
</ColumnsDirective>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
const formatOption: object = {type:'date', format:'dd/MM/yyyy'};
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='90' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='180'/>
<ColumnDirective field='orderDate' headerText='Order Date' width='160' textAlign='Right' format={formatOption}/>
<ColumnDirective field='price' headerText='Price' width='90' format='c2' textAlign='Right' type='number' />
</ColumnsDirective>
</TreeGridComponent>
};
export default App;
Lock Columns
You can lock columns by using column.lockColumn
property. The locked columns will be moved to the first position. Also you can’t reorder its position.
In the below example, duration column is locked and its reordering functionality is disabled.
import { ColumnDirective, ColumnsDirective, Inject, Reorder, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './custom.css';
import { sampleData } from './datasource';
function App() {
const customAttributes = { class: 'customcss' };
return <TreeGridComponent dataSource={sampleData} allowReordering={true} allowSelection={false} treeColumnIndex={1} childMapping='subtasks' height='315'>
<Inject services={[Reorder]}/>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='startDate' headerText='Start Date' width='110' format='yMd' textAlign='Right' type='date'/>
<ColumnDirective field='duration' headerText='Duration' width='110' lockColumn={true} textAlign='Right' customAttributes={customAttributes}/>
<ColumnDirective field='progress' headerText='Progress' width='180'/>
</ColumnsDirective>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, Inject, Reorder, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
const customAttributes: any = {class: 'customcss'};
return <TreeGridComponent dataSource={sampleData} allowReordering={true} allowSelection={false}
treeColumnIndex={1} childMapping='subtasks' height='315'>
<Inject services={[Reorder]}/>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='startDate' headerText='Start Date' width='110' format='yMd' textAlign='Right' type='date' />
<ColumnDirective field='duration' headerText='Duration' width='110' lockColumn= {true} textAlign='Right' customAttributes={customAttributes} />
<ColumnDirective field='progress' headerText='Progress' width='180'/>
</ColumnsDirective>
</TreeGridComponent>
};
export default App;
Column Type
Column type can be specified using the columns.type
property. It specifies the type of data the column binds.
If the format
is defined for a column, the column uses type
to select the appropriate format option (number or date).
TreeGrid column supports the following types:
- string
- number
- boolean
- date
- datetime
If the
type
is not defined, it will be determined from the first record of thedataSource
.
Checkbox Column
To render checkboxes in existing column, you need to set columns.showCheckbox
property as true
It is also possible to select the rows hierarchically using checkboxes in TreeGrid by enabling autoCheckHierarchy
property. When we check on any parent record checkbox then the child record checkboxes will get checked.
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='315' autoCheckHierarchy={true}>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='120' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180' showCheckbox={true}/>
<ColumnDirective field='startDate' headerText='Start Date' width='150' format='yMd' textAlign='Right' type='date'/>
<ColumnDirective field='duration' headerText='Duration' width='130' textAlign='Right'/>
</ColumnsDirective>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks'
height='315' autoCheckHierarchy={true}>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='120' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180' showCheckbox={true}/>
<ColumnDirective field='startDate' headerText='Start Date' width='150' format='yMd' textAlign='Right' type='date'/>
<ColumnDirective field='duration' headerText='Duration' width='130' textAlign='Right'/>
</ColumnsDirective>
</TreeGridComponent>
};
export default App;
For hierarchy selection between the records, we need to enable the
autoCheckHierarchy
property.
Controlling TreeGrid actions
You can enable or disable treegrid action for a particular column by setting the allowFiltering
, and allowSorting
properties.
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { Filter, Inject, Sort } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' allowSorting={true} allowFiltering={true} height='270'>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right' allowSorting={false}/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date' allowFiltering={false}/>
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right'/>
</ColumnsDirective>
<Inject services={[Filter, Sort]}/>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { Filter, Inject, Sort } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks'
allowSorting={true} allowFiltering={true} height='270'>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right' allowSorting={false}/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date' allowFiltering={false} />
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right' />
</ColumnsDirective>
<Inject services={[Filter,Sort]}/>
</TreeGridComponent>
};
export default App;
Show/Hide Columns by External Button
You can show or hide treegrid columns dynamically using external buttons by invoking the showColumns
or hideColumns
method.
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { Inject, Page } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
let treegrid;
const show = () => {
if (treegrid) {
treegrid.showColumns(['Task ID', 'Duration']); // show by HeaderText
}
};
const hide = () => {
if (treegrid) {
treegrid.hideColumns(['Task ID', 'Duration']); // hide by HeaderText
}
};
return (<div>
<ButtonComponent cssClass='e-flat' onClick={show}>Show</ButtonComponent>
<ButtonComponent cssClass='e-flat' onClick={hide}>Hide</ButtonComponent><TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='270' ref={g => treegrid = g}>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date'/>
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right'/>
</ColumnsDirective>
<Inject services={[Page]}/>
</TreeGridComponent>
</div>);
}
;
export default App;
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { Inject, Page, TreeGrid } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
let treegrid: TreeGridComponent | null;
const show = () => {
if (treegrid) {
treegrid.showColumns(['Task ID', 'Duration']); // show by HeaderText
}
}
const hide = () => {
if (treegrid) {
treegrid.hideColumns(['Task ID', 'Duration']); // hide by HeaderText
}
}
return (<div>
<ButtonComponent cssClass= 'e-flat' onClick= { show }>Show</ButtonComponent>
<ButtonComponent cssClass= 'e-flat' onClick= { hide }>Hide</ButtonComponent><TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='270' ref={g => treegrid = g}>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date' />
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right' />
</ColumnsDirective>
<Inject services={[Page]}/>
</TreeGridComponent>
</div>)
};
export default App;
ValueAccessor
The valueAccessor
is used to access/manipulate the value of display data. You can achieve custom value formatting by using the valueAccessor
.
import { getValue } from '@syncfusion/ej2-base';
import { ColumnDirective, ColumnsDirective, Inject, Page, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
const currencyFormatter = (field, data, column) => {
return '€' + getValue('price', data);
};
const orderFormatter = (field, data, column) => {
return data[field] + '-' + getValue('Category', data);
};
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='90' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='210' valueAccessor={orderFormatter}/>
<ColumnDirective field='orderDate' headerText='Order Date' width='110' format='yMd' textAlign='Right' type='date'/>
<ColumnDirective field='price' headerText='Price' width='80' valueAccessor={currencyFormatter} textAlign='Right' type='number'/>
</ColumnsDirective>
<Inject services={[Page]}/>
</TreeGridComponent>;
}
;
export default App;
import { getValue } from '@syncfusion/ej2-base';
import { ColumnDirective, ColumnsDirective, Inject, Page, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
const currencyFormatter = (field: string, data: object, column: object) => {
return '€' + getValue('price', data);
}
const orderFormatter = (field: string, data: object, column: object) => {
return data[field] + '-' + getValue('Category', data);
}
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='90' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='210' valueAccessor={orderFormatter}/>
<ColumnDirective field='orderDate' headerText='Order Date' width='110' format='yMd' textAlign='Right' type='date' />
<ColumnDirective field='price' headerText='Price' width='80' valueAccessor={currencyFormatter} textAlign='Right' type='number' />
</ColumnsDirective>
<Inject services={[Page]}/>
</TreeGridComponent>
};
export default App;
Display Array type Columns
You can bind an array of objects in a column by using the valueAccessor
property. In this example, the name field has an array of two objects, FirstName and LastName. These two objects are joined and bound to a column using the valueAccessor
.
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { stringData } from './datasource';
function App() {
const orderFormatter = (field, data, column) => {
return data[field].map((s) => {
return s.lastName || s.firstName;
}).join(' ');
};
return <TreeGridComponent dataSource={stringData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='110' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='name' headerText='Assignee' width='150' textAlign='Left' valueAccessor={orderFormatter}/>
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right'/>
</ColumnsDirective>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { stringData } from './datasource';
function App() {
const orderFormatter = (field: string, data: object, column: object): string => {
return data[field].map((s: {lastName: string, firstName: string}): string => {
return s.lastName || s.firstName }).join(' ');
}
return <TreeGridComponent dataSource={stringData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='110' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='name' headerText='Assignee' width='150' textAlign='Left'
valueAccessor={orderFormatter} />
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right' />
</ColumnsDirective>
</TreeGridComponent>
};
export default App;
Expression Column
You can achieve the expression column by using the valueAccessor
property.
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
const totalPrice = (field, data, column) => {
return data.units * data.unitPrice;
};
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='110' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='210' textAlign='Left'/>
<ColumnDirective field='units' headerText='Units' width='120' textAlign='Right'/>
<ColumnDirective field='unitPrice' headerText='Unit Price' width='120' textAlign='Right' format='c2' type='number'/>
<ColumnDirective field='price' headerText='Total Price' width='120' textAlign='Right' format='c2' type='number' valueAccessor={totalPrice}/>
</ColumnsDirective>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { orderData } from './datasource';
function App() {
const totalPrice = (field: string, data: { units: number, unitPrice: number }, column: object): number => {
return data.units * data.unitPrice;
};
return <TreeGridComponent dataSource={orderData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='orderID' headerText='Order ID' width='110' textAlign='Right'/>
<ColumnDirective field='orderName' headerText='Order Name' width='210' textAlign='Left'/>
<ColumnDirective field='units' headerText='Units' width='120' textAlign='Right'/>
<ColumnDirective field='unitPrice' headerText='Unit Price' width='120' textAlign='Right'
format='c2' type='number'/>
<ColumnDirective field='price' headerText='Total Price' width='120' textAlign='Right'
format='c2' type='number' valueAccessor= {totalPrice}/>
</ColumnsDirective>
</TreeGridComponent>
};
export default App;
Render boolean value as checkbox
To render boolean values as checkbox in columns, you need to set displayAsCheckBox
property as true.
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='approved' headerText='Approved' width='90' format='yMd' textAlign='Right' displayAsCheckBox={true}/>
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right'/>
</ColumnsDirective>
</TreeGridComponent>;
}
;
export default App;
import { ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import { sampleData } from './datasource';
function App() {
return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='315'>
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='Task ID' width='90' textAlign='Right'/>
<ColumnDirective field='taskName' headerText='Task Name' width='180'/>
<ColumnDirective field='approved' headerText='Approved' width='90' format='yMd' textAlign='Right' displayAsCheckBox={true} />
<ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right' />
</ColumnsDirective>
</TreeGridComponent>
};
export default App;
You can refer to our
React Tree Grid
feature tour page for its groundbreaking feature representations. You can also explore ourReact Tree Grid example
to knows how to present and manipulate data.