有人已经尝试过吗,我的意思是有人已经为此制作了第 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/

10-12 06:50