是否有人尝试过此操作,我的意思是有人已经为此制作了第三方/扩展或补丁吗? ajax XHR对象支持读取XML数据,但我想Fancytree需要一些更改或扩展才能支持这种格式?
答案 0 :(得分:2)
您可以在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;
}