Jstree:异步加载json数据

时间:2014-07-25 15:35:06

标签: javascript jquery json asynchronous jstree

我目前正在尝试设置一个Jstree,使用json格式提供的数据从Web服务器异步加载数据。之前,我将数据存储在html页面本身,例如。通过创建<ul> </ul>结构。这很有效。

然后,关注

http://agiliq.com/blog/2011/10/how-use-jstree/

我将页面中的数据保存为Json字符串,这也很好。之后,我尝试了异步加载,遇到两个问题:

  1. 使用Jstree jsTree 1.0-rc3(在https://github.com/akshar-raaj/Using-jsTree中找到)时,树结构正确构建,但每个文件夹左侧会出现两个扩展符号,看起来很奇怪。
  2. 然后我尝试使用当前版本的Jstree,但这根本不会生成任何树,也没有给出错误消息。
  3. 这是我的html页面:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
    <html lang="en">    
    <head>    
    <title>Menu test</title>    
    <link rel="stylesheet" href="/static/jsTree/themes/default/style.min.css" />    
    <script src="/static/jquery-1.11.1.js"></script>    
    <!--<script src="/static/jsTree/jstree.js"></script>-->    
    <script src="/static/jsTree/jstree_v3.js"></script> <!-- Current Version -->    
    <script>    
        $(function() {    
            $.ajax({    
            async : true,    
            type : "GET",    
            url : "../test2.json",    
            dataType : "json",        
    
            success : function(json) {    
                createJSTrees(json);    
            },        
    
            error : function(xhr, ajaxOptions, thrownError) {    
                alert(thrownError);    
            }    
            });    
        });        
    
        function createJSTrees(jsonData) {    
            $("#menuTree").jstree({    
            "json_data" : {    
                "data" : jsonData    
            },    
            "plugins" : [ "themes", "json_data", "ui" ]    
            });    
        }        
    </script>    
    </head>    
    <body>    
    Tree:    
    <div id="menuTree" >    
    
    </div>    
    
    </body>    
    </html>
    

    这就是test2.json给出的:

    [ { "data" : "a", "children" :[ {"data":"b", "metadata":{"href":"b"}}, {"data":"c", "metadata":{"href":"c"}} ] } ]
    

    你能帮帮我吗?

    非常感谢你!

0 个答案:

没有答案