Pass fancytree data as JSON data

I want to pass data from fancytree to a common handler to 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 also tried:

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

This gives the same error. I understand this because the "tree" is not JSON. How to convert 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 do what I need, but is there no built-in function for this in fancytree?

+4
source share
1 answer

, FancyTree; http://wwwendt.de/tech/fancytree/demo/sample-api.html.

tree.ToDict(), , .

.

  // Convert the whole tree into an dictionary
  var tree = $("#tree").fancytree("getTree");
  var d = tree.toDict(true);
  alert(JSON.stringify(d));
+9

Source: https://habr.com/ru/post/1542535/


All Articles