Search results

Local Data in React TreeGrid component

29 Nov 2022 / 3 minutes to read

In Local Data binding, data source for rendering the TreeGrid control is retrieved from the same application locally.

Two types of Data binding are possible with the TreeGrid control.

  • Hierarchical Datasource binding
  • Self-Referential Data binding (Flat Data)

To bind local data to the treegrid, you can assign a JavaScript object array to the dataSource property. The local data source can also be provided as an instance of the DataManager.

By default, DataManager uses JsonAdaptor for local data-binding.

Hierarchy data source binding

The childMapping property is used to map the child records in hierarchy data source.

The following code example shows you how to bind the hierarchical local data into the TreeGrid control.

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Inject, Page, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';
function App() {
    const pageOptions = { pageSize: 7 };
    return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' allowPaging={true} pageSettings={pageOptions}>
  <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>;
}
;
export default App;
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Inject, Page, PageSettingsModel, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';

function App() {
  const pageOptions: PageSettingsModel = { pageSize: 7 };
  return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks'
  allowPaging={true} pageSettings={pageOptions}>
  <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>
};
export default App;
  • Remote data binding is not supported for Hierarchy Data.

{% endtab %}

Self-Referential data binding (Flat data)

TreeGrid is rendered from Self-Referential data structures by providing two fields, ID field and parent ID field.

  • ID Field: This field contains unique values used to identify nodes. Its name is assigned to the idMapping property.
  • Parent ID Field: This field contains values that indicate parent nodes. Its name is assigned to the parentIdMapping property.
Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Inject, Page, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { projectData } from './datasource';
function App() {
    const pageOptions = { pageSize: 7 };
    return <TreeGridComponent dataSource={projectData} treeColumnIndex={1} idMapping='TaskID' parentIdMapping='parentID' allowPaging={true} pageSettings={pageOptions}>
  <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>;
}
;
export default App;
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Inject, Page, PageSettingsModel, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { projectData } from './datasource';

function App() {
  const pageOptions: PageSettingsModel = { pageSize: 7 };
  return <TreeGridComponent dataSource={projectData} treeColumnIndex={1} idMapping='TaskID'
  parentIdMapping='parentID' allowPaging={true} pageSettings={pageOptions}>
  <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>
};
export default App;

Herewith we have provided list of reserved properties and the purpose used in TreeGrid. We recommend to avoid these reserved properties for Internal purpose(To get rid of conflicts).

Reserved keywords Purpose
childRecords Specifies the childRecords of a parentData
hasChildRecords Specifies whether the record contains child records
hasFilteredChildRecords Specifies whether the record contains filtered child records
expanded Specifies whether the child records are expanded
parentItem Specifies the parentItem of childRecords
index Specifies the index of current record
level Specifies the hierarchy level of record
filterLevel Specifies the hierarchy level of filtered record
parentIdMapping Specifies the parentID
uniqueID Specifies the unique ID of a record
parentUniqueID Specifies the parent Unique ID of a record
checkboxState Specifies the checkbox state of a record
isSummaryRow Specifies the summary of a record
taskData Specifies the main data
primaryParent Specifies the Primary data