jsTree通过ajax加载孩子

时间:2014-01-21 09:15:15

标签: javascript jquery ajax jstree

下面发布的代码通过ajax请求加载我的树的根元素。 我的树非常大,所以我无法一次加载所有项目所以我需要通过请求孩子为特定ID加载元素。

如何通过点击节点按ajax加载元素?

  $('#jstree_demo_div').jstree({
            "plugins" : ["wholerow", "checkbox"],
            'core' : {
                'data' : {
                    'url' : function(node) {
                        return "/" + site + "/places/api/tree/list/";
                    }
                },
            }

        });

json样本的一部分

[
   {
      "text":"zachodniopomorskie",
      "state":"closed",
      "id":212353,
   },

修正版:

 $('#jstree_demo_div').jstree({
            "plugins" : ["wholerow", "checkbox"],
            'core' : {
                'data' : {
                    'url' : "/" + site + "/places/api/tree/list/",
                    'data' : function(node) {
                        return {
                            'id' : node.id
                        };
                    }
                },
            }
        })

我的问题的解决方案是,如果我想通过ajax请求返回子项,我需要返回包含以下内容的json文件:

"children:" true

3 个答案:

答案 0 :(得分:2)

 $('#jstree_demo_div').jstree({
            "plugins" : ["wholerow", "checkbox"],
            'core' : {
                'data' : {
                    'url' : "/" + site + "/places/api/tree/list/",
                    'data' : function(node) {
                        return {
                            'id' : node.id
                        };
                    }
                },
            }
        })

我的问题的解决方案是,如果我想通过ajax请求返回子项,我需要返回包含以下内容的json文件:

"children:" true

答案 1 :(得分:1)

试试这个:

$('#jstree_demo_div').jstree(options).bind("select_node.jstree",function(event, data){
//Load child node here 

});//or "dbclick.jstree" instead of "select_node.jstree"

答案 2 :(得分:0)

如果您需要加载子节点,可以尝试使用

$("#jstree_demo_div").bind("select_node.jstree", function(e, data) {
    $("#jstree_demo_div").jstree('open_node', data.node);
}

所以它会触发一个ajax加载触发器。