How to get data from tree?

3,178 views
Skip to first unread message

Mohammad Zakir Hussain

unread,
Jun 11, 2016, 12:40:23 PM6/11/16
to jsTree
hi

How to get data from JsTree to save it to backend?

Thanks in advance...

Regards,
MZ

Alejandro Grigera

unread,
Jun 23, 2016, 9:49:39 AM6/23/16
to jsTree
What do you want to get from the nodes?

$("#jstree_div").jstree().get_node(node).text
$("#jstree_div").jstree().get_node(node).data.someinfo
$("#jstree_div").jstree().get_node(node).children

This are some of the data you can get from the nodes, of course you need the node or at least the node id.
If you use get_json function you can even get a json of specific node or the whole tree.

Hope this helps

Mohammad Zakir Hussain

unread,
Jun 24, 2016, 4:16:54 AM6/24/16
to jsTree
Sir Alejandro,

I am using below logic to get JSON data from JSTree  after getting specific data then storing it to localstorage and from there to webserver, I'm not sure whether
i'm doing wrong or right. Please suggest?



$(function() {
$('#container1').jstree({
   'core' : {
       "animation" : 0,
       "check_callback" : true,
       "themes" : { "stripes" : true },
      'data' : [{"id":"Celulares","text":"Celulares","parent":"#"},
                {"id":"AAA","text":"AAA","parent":"Celulares"},
                {"id":"Test1","text":"Test1","parent":"#"},
                {"id":"BBB","text":"BBB","parent":"Test1"},
                {"id":"CCC","text":"CCC","parent":"#"},
                ]
},
"plugins" : [ "dnd", "contextmenu", "massload", "search", "unique"]
});
$('button').on("click", function () {
window.localStorage.clear();

if (typeof(Storage) !== "undefined") {
var v = $('#container1').jstree(true).get_json('#', {no_state:true,flat:true})
//var mytext = JSON.stringify(v);
//alert(mytext);
   var json_obj = $.parseJSON(JSON.stringify(v));
            
            for (var i in json_obj)  
            { 
            localStorage.setItem("K"+i, json_obj[i].id+'|'+json_obj[i].text+'|'+json_obj[i].parent);
            } 
} else {
    alert("Sorry! No Web Storage support..");
}
document.getElementById("result").innerHTML = localStorage.getItem("K1");
});
});

Thanks in advance...
Regards,
Mohammad

Alejandro Grigera

unread,
Jun 24, 2016, 4:40:33 PM6/24/16
to jsTree
Ok, I don't really know why you use the localstorage, hope you know what your doing, but I think you should try this:

...

if (typeof(Storage) !== "undefined") {
    var v = $('#container1').jstree().get_json('#', {no_state:true,flat:true})

var json_obj = JSON.stringify(v);     //This is all you need
   
    $.each(json_obj , function( index, value ) {     //Same as the for but i think is a little bit more elegant, if you dont like it go back to the for
        localStorage.setItem("K"+index, value.id+'|'+value.text+'|'+value.parent);
    });
} else {
...

Please try it and let me know if it works.
Best regards

Mohammad Zakir Hussain

unread,
Jun 25, 2016, 11:47:24 AM6/25/16
to jsTree
Thanks Alejandro its working fine now...
Reply all
Reply to author
Forward
0 new messages