fancytree - 在 Fancytree 中使用 XML 数据而不是 JSON 数据

标签 fancytree

有人已经尝试过了吗,我的意思是有人已经为此制作了第 3 方/扩展或补丁吗? ajax XHR 对象支持读取 XML 数据,但我想 Fancytree 需要一些更改或扩展来支持这种格式吗?

最佳答案

您可以在 postProcess 事件中解析和转换 XML 响应。

举个例子,假设这个 XML 格式:

<children>
    <node>
        <title> Node 1</title>
    </node>
    <node folder="true" expanded="true" key="42">
        <title> Node 2 (expanded folder)</title>
        <children>
            <node>
                <title> Node 2.1</title>
            </node>
            <node>
                <title> Node 2.2</title>
            </node>
        </children>
    </node>
</children>

树可以像这样转换 ajax 响应:

$("#tree").fancytree({
    source: { url: "ajax-tree.xml", dataType: "xml" },
    lazyLoad: function(event, data) {
        data.result = { url: "ajax-sub.xml", dataType: "xml" };
    },
    postProcess: function(event, data) {
        // Convert the xml responses to a Fancytree NodeData list.
        // data.response is a `#document` root, so we get the outer
        // `<children>` element:
        data.result = parseFancytreeXml($(">children", data.response));
    }
});

最后缺少示例格式转换器:

/** Return a list of NodeData objects, assuming $xml points to a list of nodes.
 */
function parseFancytreeXml($xml) {
    var children = [];

    $xml.children("node").each(function() {
        var $node = $(this),
            subnodes = $node.children("children");

        // Create Fancytree NodeData object from <node> element
        children.push({
            title: $node.children("title").text(),
            expanded: $node.attr("expanded"),
            folder: $node.attr("folder"),
            key: $node.attr("key"),
            lazy: $node.attr("lazy"),
            children: subnodes.length ? parseFancytreeXml(subnodes) : null
        });
    });
    return children;
}

关于fancytree - 在 Fancytree 中使用 XML 数据而不是 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28431135/

相关文章:

c# - Fancytree 没有加载 ajax 请求

jquery - 如何在 fancytree jquery 中获取树元数据

jquery - 使用列 View ext 时是否可以设置 fancyTree 的高度

javascript - 将 fancytree 节点拖到外部 droppable

javascript - fancytree tr 类坚持

fancytree - 动态更改花式树中的图标

javascript - jQuery 插件生成的 Aurelia 路由链接

javascript - Fancytree:手动重新生成 key 以避免重复

javascript - 如何获取 fancytree 中某个节点的所有子节点?

javascript - 如何区分 fancytree 中的展开与点击?