【问题标题】:load data from external json file in jstree从 jstree 中的外部 json 文件加载数据
【发布时间】:2016-05-06 07:21:27
【问题描述】:

我想将 json 数据从外部文件传递给 jstree 对象。 我有关注 sn-p 但它不起作用

<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 外部文件的路径时,它就不起作用了

【问题讨论】:

    标签: javascript json jstree


    【解决方案1】:

    您使用的是哪个版本的 jstree?当前版本是 3.3.1。旧版本有一个名为 json_data 的插件,您需要在插件列表中包含“json_data”。如果使用最新版本,则不需要使用“json_data”,只需使用“url”即可。请参见下面的示例:

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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多