The*_*ude 5 javascript jquery json jstree asp.net-mvc-3
目前,我在 SQL Server 中有两个存储过程,用于处理从数据库检索树:
第一个存储过程在您传入级别编号时检索特定级别的所有节点。
当您传入 Level、Left 和 Right 值时,另一个会检索特定节点的子节点。
我正在使用 MVC 3。
理想情况下,我想设置 JSTree 以在每次用户单击展开节点时调用数据库。因此,我不想像通常那样将整个树加载到服务器上的 Json 模型中,然后将其传递给 JSTree,而是只传递用户单击的特定节点的子节点。这将在每个节点上发生,因此只有直接节点的子节点才必须传递到 JSTree 而不是整个树。
这可能吗?如果是这样,我会很感激一些视图(尤其是)的示例代码,并且可能还有使用 Microsoft 的 MVC 3 框架的控制器。我将不胜感激任何帮助。
是的,这是可能的,实际上使用 jstree 非常简单。
您想要做的是使用jstree 插件ajax的参数json_data,但对其进行设置,以便将data或url参数传递给一个函数,该函数将发送已扩展的节点 ID,以便您的 Web 服务可以调用您的存储过程并返回所选节点的子节点的数据。
这是http://www.jstree.com/documentation/json_data中的示例之一,稍作修改:
$("#tree").jstree({
"json_data" : {
"ajax" : {
"url" : "/yourwebservice/getnodechildren",
"data" : function (node) {
//this is passed the node being opened
//or -1 if it's the root node
var dataToPass = {};
if (node === -1) {
//pass parameters to the webservice that will build and return
//first two tree levels
dataToPass = {
id : 0,
initialLoad: true
};
}
if (node.attr && node.attr("id") {
dataToPass = {
id: node.attr("id"),
initialLoad: false
}
}
return dataToPass;
},
"success" : function (dataFromWebservice) {
//depending on how the webservice returns
//data you may need to do this
return dataFromWebservice.d;
}
}
},
"plugins" : [ "themes", "json_data" ]
});
Run Code Online (Sandbox Code Playgroud)
您可以使这段代码更加优雅,但这就是要点。这将允许您根据需要分块构建树,而不是一次性构建树。
如果您的 Web 服务设置为在 url 上传递参数,则只需将 url 设为函数,然后使用该函数通过节点 id 或您需要的任何其他参数来构建您的 url 请求。