javascript jsTree 通过 ajax 加载子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21253710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:28:45  来源:igfitidea点击:

jsTree load children by ajax

javascriptjqueryajaxjstree

提问by Efrin

Code posted below loads root elements for my tree by ajax request. My tree is very large so I can't load all items at once so I need to load elements by requesting children for specific ID's.

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

How do I load elements by ajax by clicking on node?

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

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

        });

Part of json sample

部分 json 示例

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

Fixed version:

固定版本:

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

The solution to my problem is that if I want to return children by ajax request I need to return json file which contains:

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

"children:" true

采纳答案by Efrin

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

The solution to my problem is that if I want to return children by ajax request I need to return json file which contains:

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

"children:" true

回答by Yang Kul

Try this :

试试这个 :

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

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

回答by Drath Vedro

If you need to load child node you may try using

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

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

so it would fire an ajax load trigger.

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