我试图在 jtree 扩展时动态加载它们的节点。我发现的小文档位于末尾这一页 http://www.jstree.com/docs/json/.
我找到了一些解决方案,可以使用类似的循环逐一创建节点this one https://stackoverflow.com/questions/14952653/programatically-expanding-nodes-in-jstree-with-ajax-load。我还没有尝试过,但是查看文档页面,我感觉 jstree 应该负责循环遍历节点。
我发现很多解决方案都使用plugins: ["json_data"]
,但是插件文档页面 http://www.jstree.com/plugins/根本没有提到那个插件。这是一个不再需要的旧插件吗?
我当前的实现使用此代码一次性加载整个树:
$.ajax({
var pn = $('#project_number').val();
url : "bomtree?part=" + pn,
success : function(tree_content) {
var data = $.parseJSON(tree_content);
var config = {
'core' : {
'data' : data
}
};
$('#bom_tree').jstree(config);
}
});
我修改了文档页面上的代码,如下所示:
$(function() {
var pn = $('#project_number').val();
$('#tree').jstree({
'core' : {
'data' : {
'url' : function(node) {
return '/doc/test2';
},
'data' : function(node) {
return {
'part' : node.id === '#' ? pn : node.id
};
}
}
}
});
});
相同的 json 文本适用于第一个代码,现在适用于第二个代码。文档说The format remains the same as the above
,所以我没有改变它。
我也尝试返回与示例中相同的数据,如下:
[
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
但结果是相同的:jquery 在以下行抛出 Sizzle.error:
Sizzle.error = function( msg ) {
throw new Error( "Syntax error, unrecognized expression: " + msg );
};
内容在哪里msg
是服务器返回的json数据。
怎么了?