简体   繁体   English

Fancytree:如何从ext-table获取内容

[英]Fancytree: How to get content from ext-table

I'm (for the first time) using fancytree . 我(第一次)使用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. 最后,我想将整个树转换为一些JSON并将其make发送到服务器。

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. 问题在于,在遍历树期间,“ fancytree-active”未添加到当前遍历的节点。

So my question could be formulated: How can I in the context of the toDict() callback access the html-object for the given node? 因此,可以提出我的问题:如何在toDict()回调的上下文中访问给定节点的html对象?

If this is not possible, is there another way of reading the tree besides reading the whole tr and doing the extraction by hand? 如果无法做到这一点,除了读取整个tr并手动进行提取之外,还有其他读取树的方法吗?

在遍历期间,您可以通过node.tr访问节点的<tr> ,因此您的代码可能像这样(未经测试)工作:

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

I found a solution thanks to mar10 answer. 由于mar10的答案,我找到了解决方案。

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 . 说明:如前所述,在toDict()回调中,每个节点只有3个值可用: titlekeyfolder But when you use visit() instead of toDict() you get access to the full node object. 但是,当您使用visit()而不是toDict()时,您就可以访问整个节点对象。 So what my solution does is, that it first collects all attributes during a normal tree traversal, stores it in the mapKeytoAttr map. 因此,我的解决方案要做的是,它首先在正常的树遍历过程中收集所有属性,然后将其存储在mapKeytoAttr映射中。 And then in the second run, I use toDict() grabbing the attributes from that map. 然后在第二次运行中,我使用toDict()从该映射中获取属性。

@mar10: fancytree is a very nice piece of software! @ mar10:fancytree是一个非常不错的软件!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM