【问题标题】:Fancytree: How to get content from ext-tableFancytree:如何从 ext-table 获取内容
【发布时间】:2015-07-17 03:33:25
【问题描述】:

我(第一次)使用fancytree。它似乎很适合我的问题,但有一个问题我被困住了。

我正在使用表格扩展来让用户向树的节点添加额外的信息。最后,我想将整个树转换为一些 JSON 并将其发送到服务器。

到目前为止,我的代码如下所示:

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);
}

问题是,在树遍历期间,“fancytree-active”没有添加到当前遍历的节点。

所以我的问题可以表述为: 如何在 toDict() 回调的上下文中访问给定节点的 html 对象?

如果这不可能,除了读取整个 tr 并手动提取之外,还有其他读取树的方法吗?

【问题讨论】:

    标签: javascript fancytree


    【解决方案1】:

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

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

    【讨论】:

    • 嘿,谢谢。不幸的是,回调是使用仅包含成员 folderkeytitle 的新节点对象调用的。
    • 你说得对,我应该知道得更好 ;-) 但它应该那样工作。我为此打开了一个功能请求:github.com/mar10/fancytree/issues/465
    • 我可以反其道而行之吗?从 tr 中获取节点?
    【解决方案2】:

    感谢 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;
    }
    

    解释:如评论所述,在 toDict() 回调中,每个节点只有 3 个值可用:titlekeyfolder。但是当您使用 visit() 而不是 toDict() 时,您可以访问完整的节点对象。所以我的解决方案是,它首先在正常的树遍历期间收集所有属性,将其存储在mapKeytoAttr 映射中。 然后在第二次运行中,我使用toDict() 从该地图中获取属性。

    @mar10:fancytree 是一款非常不错的软件!

    【讨论】:

      猜你喜欢
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多