目前,我在SQL Server中有两个存储过程来处理从数据库中检索树:
当您传入一个级别编号时,第一个方法检索特定级别的所有节点。
另一个方法在传入Level、Left和Right值时检索特定节点的子节点。
我使用的是MVC3。
理想情况下,我希望将JSTree设置为在用户每次单击以展开节点时调用数据库。因此,我希望只传递用户单击的特定节点的子节点,而不是像通常那样将整个树加载到服务器上的Json模型中,然后将其传递给JSTree。这将在每个节点上发生,因此只有直接节点的子节点必须传递到JSTree中,而不是整个树中。
这个是可能的吗?如果是这样的话,我希望能为视图(特别是)以及使用微软的MVC3框架的控制器提供一些示例代码。如果能帮上忙我会很感激的。
发布于 2012-10-30 23:22:59
是的,这是可能的,使用jstree实际上是非常简单的。
您要做的是使用webservice插件的ajax
参数,但对其进行设置,以便向data
或url
参数传递一个函数,该函数将发送已展开的节点id,以便您的set服务可以调用您的存储过程并返回所选节点的子节点的数据。
下面是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" ]
});
您可以使这段代码更优雅一些,但这就是要点。这将允许您按需分块构建树,而不是一次性构建所有树。
如果您的webservice设置为在url上传递参数,则只需将url设置为函数,并使用该函数来构建您的url请求,而不是使用节点的id或您需要的任何其他参数。
https://stackoverflow.com/questions/10859266
复制相似问题