Chirs
Chirs

Reputation: 577

Fancytree: How to get content from ext-table

I'm (for the first time) using fancytree. It seems well-suited for my problems, but there is one problem which I'm stuck on.

I'm using the table extension to let users add additional information to the nodes of the tree. At the end I want to convert the whole tree to some JSON and send it make to the server.

My code so far looks like this:

function readTree(tree) {
    var d = tree.toDict(true, function(node){
        console.log('looking at ' + node.title);
        var tdList = $('tr.fancytree-active>td');
        /* read the attributes */
        node.attr = { 
            ctime : tdList.eq(2).find("input").val(),
            filesize : tdList.eq(3).find("input").val(),
            user : tdList.eq(4).find("input").val(),
            group : tdList.eq(5).find("input").val(),
            permissions : tdList.eq(6).find("input").val()
        };
    });
    console.log(d);
}

The problem is, that during the tree traversal the "fancytree-active" is not added to the currently traversed node.

So my question could be formulated: How can I in the context of the toDict() callback access the html-object for the given node?

If this is not possible, is there another way of reading the tree besides reading the whole tr and doing the extraction by hand?

Upvotes: 0

Views: 671

Answers (2)

Chirs
Chirs

Reputation: 577

I found a solution thanks to mar10 answer.

function readTree(tree) {
    /* first: store all attributes in a map (accessible with the key) */
    window.mapKeytoAttr = {}; 

    tree.visit(function(node) {
        var tdList = $(">td", node.tr);
        attrs = { 
            mime : tdList.eq(1).find("input").val(),
            ctime : tdList.eq(2).find("input").val(),
            filesize : tdList.eq(3).find("input").val(),
            user: tdList.eq(4).find("input").val(),
            group: tdList.eq(5).find("input").val(),
            permissions: tdList.eq(6).find("input").val()
        };
        window.mapKeytoAttr[node.key] = attrs;
    }); 

    /* second: use treeToDict() as before, but read attributes from the map */
    var d = tree.toDict(true, function(node) {
        node["attrs"] = window.mapKeytoAttr[node.key];
    }); 
    return d;
}

Explanation: As commented, in the toDict() callback there are only 3 values available for each node: title, key and folder. But when you use visit() instead of toDict() you get access to the full node object. So what my solution does is, that it first collects all attributes during a normal tree traversal, stores it in the mapKeytoAttr map. And then in the second run, I use toDict() grabbing the attributes from that map.

@mar10: fancytree is a very nice piece of software!

Upvotes: 0

mar10
mar10

Reputation: 14794

During traversal you can access the node's <tr> by node.tr, so you code may work like this (untested):

var tdList = $(">td", node.tr);

Upvotes: 1

Related Questions