Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / Javascript
Tip/Trick

Serialize Kendo Treeview Data in JSON

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
26 Aug 2013CPOL 30.7K   1   4
How to serialize a kendo treeview current view into JSON data

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Introduction

There are times that you may want to serialize your kendo treevew's view and store it somewhere so you can load it back later. This article provides a quick and simple way to serialize the treeview data by using a few lines of jQuery code.

Use Case

In our use case, we have a treeview with checkboxes to display heirarchical data. Users may select items they want in the treeview by checking the checkboxes and save them as part of their preference settings. Once it's saved, we also need to be able to load it back from the data.

Using the code

The test HTML code has just the treeview div section:

JavaScript
<body>
  <div id="treeview"></div>
</body>

The jQuery code is pretty simple too:

JavaScript
var ds = new kendo.data.HierarchicalDataSource({
    data: [{"text":"Item 1","id":"1","expanded":true,
      "checked":true,"items":[{"text":"Item 1.1",
      "id":"2","checked":true},{"text":"Item 1.2",
      "id":"3","checked":true},{"text":"Item 1.3",
      "id":"4","checked":true}]},{"text":"Item 2",
      "id":"5","expanded":true,"items":[{"text":"Item 2.1",
      "id":"6","checked":true},{"text":"Item 2.2",
      "id":"7"},{"text":"Item 2.3","id":"8",}]},
      {"text":"Item 3","id":"9"}]
});

var tv = $("#treeview").kendoTreeView({
    checkboxes: {
        checkChildren: true
    },
    dataSource: ds
}).data("kendoTreeView");


function treeToJson(nodes) {

    return $.map(nodes, function(n, i) {
    
        var result = { text: n.text, id: n.id, expanded: n.expanded, checked: n.checked };
    
        

        if (n.hasChildren)
            result.items = treeToJson(n.children.view());

        return result;
    });
}

var json = treeToJson(tv.dataSource.view()); 
console.log(JSON.stringify(json));

The function treetoJson() will loop through all the tree nodes and return the array of data in JSON.

You can test it on JS Bin. Make sure you include "Kendo UI" by clicking on the "Add Library" link.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Linus has more than 10 years of experience in designing and implementing enterprise scale applications. He is a seasoned architect in both J2EE and Microsoft technologies. He is also a Microsoft Certified Solution Developer for .NET.

Comments and Discussions

 
QuestionThanks for this great tip! Pin
Bob Chauvin17-Mar-20 5:32
Bob Chauvin17-Mar-20 5:32 
AnswerAwesome! Pin
axwack12-Dec-15 4:54
axwack12-Dec-15 4:54 
QuestionThank you...vote of 5 Pin
Troy compton20-Dec-13 10:09
Troy compton20-Dec-13 10:09 
GeneralMy vote of 5 Pin
Ibrahim Islam8-Sep-13 2:23
Ibrahim Islam8-Sep-13 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.