从jstree中的外部json文件加载数据

时间:2016-05-06 07:21:27

标签: javascript json jstree

我想将json数据从外部文件传递给jstree对象。 我有以下片段,但它不起作用

<script>
            $.jstree.defaults.core.themes.responsive = true;
$('#frmt').jstree({
    plugins: ["checkbox", "types"],
    "json_data":{
    "ajax" : {
        "url" : "D:\p\web\nodes.json"  // the URL to fetch the data. Use relative url if required
     }
},
       "types": {
        "file": {
            "icon": "jstree-file"
        }
    }
});
</script>

我的nodes.json文件

[
 {
  "id": "ajson1",
  "parent": "#",
  "text": "Simple root node"
}, 
{
  "id": "ajson2",
  "parent": "#",
  "text": "Root node 2"
}, {
  "id": "ajson3",
  "parent": "ajson2",
  "text": "Child 1"
}, {
  "id": "ajson4",
  "parent": "ajson2",
  "text": "Child 2"
}
]

当我在javascript中手动插入这些数据时,它工作正常,但是当我指定nodes.json外部文件的路径时,它没有工作

1 个答案:

答案 0 :(得分:3)

您使用的是哪个版本的jstree?当前版本是3.3.1。旧版本有一个名为json_data的插件,你需要包含&#34; json_data&#34;在你的插件列表中。如果使用最新版本,您不需要使用&#34; json_data&#34;,您只需使用&#34; url&#34;。见下面的例子:

$('#tree').jstree({
    'core' : {
        'data' : {
            'url' : function (node) {
                return node.id === '#' ? 'ajax_roots.json' : 'ajax_children.json';
             },
            'data' : function (node) {
                return { 'id' : node.id };
            }
         }
    });

希望有所帮助。

相关问题