Understanding Hierarchical and Self-Referential JSON Data Binding in Tree Grid | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (118)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (914)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Understanding Hierarchical and Self-Referential JSON Data Binding in Tree Grid

Understanding Hierarchical and Self-Referential JSON Data Binding in Tree Grid

Syncfusion’s Tree Grid is a high-performance tree table component that visually represents data with hierarchical relations in it. To visually represent data in a tree grid, we first need to provide the data to the Tree Grid component. In tree grid JSON data binding, you can choose between two structures of JSON data that can be bound to the Tree Grid control:

In this blog, we will explain the two JSON structures so that you can have a clear picture of them. It will help you decide when and how to use them in your application.

Hierarchical data

Hierarchical data, as the name says, is when the data is in a hierarchical structure, i.e., a nested collection. In hierarchical data binding, all applicable parent records in a collection have child records as nested collections.

Consider the following sampleData collection. It contains the details of a task and its subtasks. Every task has its subtask collection nested under the property named subtasks.

So sampleData contains a collection of records of tasks and each record of tasks has a nested collection of subtasks. So, data that has nested collections for its child records is called hierarchical data and that method of binding is known as hierarchical data binding.

export var sampleData =  [
    {
        taskID: 1, taskName: 'Planning', startDate: new Date('02/03/2017'), duration: 5, subtasks: [
            { taskID: 2, taskName: 'Plan timeline', startDate: new Date('02/03/2017'), duration: 5 },
            { taskID: 3, taskName: 'Plan budget', startDate: new Date('02/03/2017'), duration: 0 }
        ]
    },
    {
        taskID: 4, taskName: 'Design', startDate: new Date('02/10/2017'), duration: 3, subtasks: [
            { taskID: 5, taskName: 'Software Specification’, startDate: new Date('02/10/2017'), duration: 3 }
        ]
    }
]

For hierarchical data binding, we need to provide the nested collection property name to the childMapping field of the Tree Grid. For instance, from the previous data, we need to provide subtasks as the childMapping property of Tree Grid.

let treegrid: TreeGrid = new TreeGrid({
    dataSource: sampleData,
    treeColumnIndex: 1,
    childMapping: "subtasks",
    columns: [
      { field: "taskID", headerText: "Task ID", isPrimaryKey: true, textAlign: "Right", width: 140 },
      { field: "taskName", headerText: "Task Name", width: 160 },
      { field: "startDate", headerText: "Start Date", textAlign: "Right", width: 120, format: { skeleton: "yMd", type: "date" } },
      { field: "duration", headerText: "Duration", textAlign: "Right", width: 110 }
    ]
});

Self-referential data

While working in the backend, like with databases, we would not use hierarchical data (nested collections) in most situations. And self-referential databinding comes handy in situations like this to provide a tree structure without nested collections.

Self-referential data consists of flat records and doesn’t have nested collections to hold child records. Instead, parent and child relationships are represented in the properties of the record.

Each record should have a unique field that identifies a child record’s parent record.

Let’s see self-referential data with same content as used with hierarchical data.

var projectData = [
    { 'TaskID': 1, 'TaskName': 'Planning', 'StartDate': new Date('02/23/2017'), 'Duration': 5 },

    { 'TaskID': 2, 'TaskName': 'Plan timeline', 'StartDate': new Date('02/23/2017'), 'Duration': 5,  'parentID': 1 },

    { 'TaskID': 3, 'TaskName': 'Plan budget', 'StartDate': new Date('02/23/2017'), 'Duration': 0,  'parentID': 1,  },

    { 'TaskID':4, 'TaskName': 'Design', 'StartDate': new Date('02/10/2017'), 'Duration': 3 },

    { 'TaskID':5, 'TaskName': 'Software Specification','StartDate': new Date('02/10/2017'), 'Duration': 3, 'parentID': 4 }]

From this data, you can see TaskID is the unique field in every record and it is set as the idMapping property in the tree grid. The parentID in some records denotes that they are child records to the TaskID of a value that equals the parentID field value. Thus, the parentID field is mapped to the parentIdMapping property of Tree Grid.

Refer to the following code example.

let treegrid: TreeGrid = new TreeGrid({
    dataSource: projectData,
    treeColumnIndex: 1,
    idMapping: "TaskID",
    parentIdMapping: "parentID",
    columns: [
      { field: "TaskID", headerText: "Task ID", isPrimaryKey: true, textAlign: "Right", width: 140 },
      { field: "TaskName", headerText: "Task Name", width: 160 },
      { field: "StartDate", headerText: "Start Date", textAlign: "Right", width: 120, format: { skeleton: "yMd", type: "date" } },
      { field: "Duration", headerText: "Duration", textAlign: "Right", width: 110 }
    ]
});

Output

Output

Note: Hierarchical and self-referential data binding look the same in the UI and only the data structure representation is different in JSON.

Conclusion

I hope you now have a clear idea of the difference between hierarchical and self-referential data binding. The Syncfusion Tree Grid control is available in our Blazor, ASP .NET (Core, MVC, Web Forms), JavaScript, Angular, React, Vue , UWP, WPF, and WinUI platforms.

If you would like to try our Tree Grid component, you can download our free trial. You can check our sample browser and documentation for detailed explanations and the facts you need to proceed further.

Feel free to share your feedback or questions in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed