Question

can you please tell me how to make json object from tree view .I am using jstree.I need to get json object when user "get json " object.

here is my expected out put of my demo : http://jsfiddle.net/fuu94/54/

[
                        {
                            "id" : "a",

                            "children" : []
                        },
                        {
                            "id" : "b",

                            "children" : [ {"id":"b-a-1", "children" : []}, {"id":"b-a-2", "children" : {"id":"b-b-a", "children" : []},{"id":"b-b-b", "children" : []}}]
                        },

                        {
                            "id" : "c",

                            "children" : [ {"id":"c-a-1", "children" : []}, {"id":"c-b-2", "children" : []}]
                        },
                    ]
Was it helpful?

Solution

have you seen serialize-list plugin ?

thank you for downvotes :P

function jsonify() {
    var $this = $(this);
    return {
        id: $this.attr('id'),
        children: $this.find('ul > li').map(jsonify).get()
    };
}

$('#json').click(function(){
    var json_list = $('#tree > ul > li').map(jsonify).get();
    console.log(JSON.stringify(json_list));
});

this is working for me, but not with that tree plugin, because it changes DOM !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top