javascript – 使用存储过程按需加载JSTree

目前,我在SQL Server中有两个存储过程来处理从数据库中检索树:

传入级别编号时,第一个检索特定级别的所有节点.

另一个在传递Level,Left和Right值时检索特定节点的子节点.

我正在使用MVC 3.
理想情况下,我想设置JSTree,以便每次用户单击以展开节点时调用数据库.因此,我不想将整个树加载到服务器上的Json模型中,然后将其传递给JSTree,而是仅传递用户单击的特定节点的子节点.这将发生在每个节点上,因此只有直接节点的子节点必须传递到JSTree而不是整个树.

这可能吗?如果是这样,我会欣赏视图的一些示例代码(尤其是)以及可能使用Microsoft MVC 3框架的控制器.我很感激任何帮助.

最佳答案 是的,这是可能的,它实际上非常简单的jstree.

你想要做的是使用json_data jstree插件的ajax参数,但是设置它以便数据或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" ]
});

你可以使这段代码更加优雅,但这就是要点.这将允许您按块而不是一次性构建树.

如果您的web服务已设置为在url上传递参数,则只需将url作为函数,然后使用该函数来构建具有节点id的url请求,或者您需要的其他任何参数.

点赞