Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass fancytree data as JSON data

I want to pass the data from a fancytree to a generic handler so that I can save it for future use.

If I use this code:

function SaveTree() {
    var tree = $('#TopTree').fancytree("getTree");
    $.ajax({
        cache: false,
        url: "SaveTree.ashx",
        data: { 'treeData': tree },
        contentType: "application/json; charset=utf-8"
    });
}

Then I get the following error from jquery.js:

JavaScript runtime error: Argument not optional

I have also tried:

var tree = $('#TopTree').fancytree("getTree").rootNode.children;

This gives the same error. I understand it is because 'tree' is not JSON. How can I convert the data to a JSON object?

EDIT:

If I use this code:

function SaveTree() {
    var data = [];
    var tree = $('#TopTree').fancytree("getTree").rootNode.children;
    for (var i = 0; i < tree.length; i++) {
        data.push(tree[i].title)
    }
    data = JSON.parse(JSON.stringify(data))
    $.ajax({
        cache: false,
        url: "SaveTree.ashx",
        data: { 'treeData': data },
        contentType: "application/json; charset=utf-8"
    });
}

I can get it to accomplish what I need, but is there not a built-in function for this in fancytree?

like image 985
Family Avatar asked May 29 '14 18:05

Family


1 Answers

Okay, I don't really know a lot about FancyTree; however, I did some investigation and found this page http://wwwendt.de/tech/fancytree/demo/sample-api.html.

Try the tree.ToDict() option to see if that is what you're looking for.

This is the source code.

  // Convert the whole tree into an dictionary
  var tree = $("#tree").fancytree("getTree");
  var d = tree.toDict(true);
  alert(JSON.stringify(d));
like image 82
Aelphaeis Avatar answered Oct 11 '22 09:10

Aelphaeis