【问题标题】:dojo: loading json data from local file (using http) into dijit treedojo:将本地文件中的 json 数据(使用 http)加载到 dijit 树中
【发布时间】:2018-04-11 08:23:07
【问题描述】:

文件 snapshot_report_js.js:

require([
    "dojo/dom",
    "dojo/json",
    "dojo/store/Memory",
    "dijit/tree/ObjectStoreModel",
    "dijit/Tree",
    "dojo/text!http://localhost:8080/dojo/json_data/snapshot.json",
    "dojo/domReady!",
    "dojo/_base/json"
], function(dom, json, Memory, ObjectStoreModel, Tree, small){
    var stringfied_content = JSON.stringify(small)
    var json_parsed_data = JSON.parse(stringfied_content, true)
    var json_data = dojo.toJson(json_parsed_data);
    // set up the store to get the tree data
    var json_store = new Memory({
        data: [ json_data ],
        getChildren: function(object){
            return object.children || [];
        }
    });
    // Create the model
    var snapshot_treeModel = new ObjectStoreModel({
        store: json_store,
        query: {id: 'snapshot'}
    });

    var snapshot_tree = new dijit.Tree({
        model: snapshot_treeModel
    }, 'div_snapshot_tree');
    snapshot_tree.startup();  
})

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Snapshot</title>
<link rel="stylesheet" href="dijit/themes/claro/claro.css">
<!-- load Dojo -->
<script src="dojo/dojo.js" data-dojo-config="async: true"></script>
<script src="js/snapshot_report_js.js"></script>
</head>
<body class="claro">
<div id="div_snapshot_tree"></div>
</body>
</html>

JSON 文件:

{
    "snapshot_metadata": {
            "report_end_at": "2017-10-11 02:03:36", 
            "environment_name": "DVINST", 
            "report_start_at": "2017-10-11 01:55:42"
    },
    "versions": [
        {
            "id": "version_001",
            "instances": [
                {
                    "instance_name": "instance1", 
                    "instance_create_date": "2017-09-18 00:17:52", 
                    "connected_site_count": 4, 
                    "admin_server": "t3://tserver:18300", 
                    "instance_id": 2411, 
                    "instance_type": "OSVC", 
                    "instance_created_by": "None", 
                    "site_capacity": 2, 
                    "sites": [
                            {
                            "site_db_id": 395, 
                            "site_name": "zzzz_178", 
                            "site_users": "uc1,uc2,uc3", 
                            "site_id": 89492, 
                            "site_owner": "owner1", 
                            "site_db_name": "site_server2"
                            },
                            {
                            "site_db_id": 395, 
                            "site_name": "site2", 
                            "site_users": "u1, u2, u3", 
                            "site_id": 90447, 
                            "site_owner": "u2", 
                            "site_db_name": "site_server3"
                            }
                    ]
                }
            ]
        }
    ],
    "servers": [
        {
            "status": null, 
            "server_id": 13, 
            "server_name": "db1", 
            "server_type": "database", 
            "mount_points": [], 
            "sites": [], 
            "db_connections_count": 6, 
            "health": null, 
            "admin_servers": null, 
            "db_sites_connected_count": null
        }
    ]
}

控制台错误:

dojo.js:8 未捕获的错误:dijit.tree.ObjectStoreModel:根查询 返回 0 个项目,但必须在 Object 处准确返回一个。
(ObjectStoreModel.js.uncompressed.js:86) 在 dojo.js:8 时
(dojo.js:8) 在 Object.getRoot
(ObjectStoreModel.js.uncompressed.js:82) 在 Object._load
(Tree.js.uncompressed.js:897) 在 Object.postCreate
(Tree.js.uncompressed.js:844) 在 Object.create
(_WidgetBase.js.uncompressed.js:453) 在 Object.postscript
(_WidgetBase.js.uncompressed.js:366) 在新 (dojo.js:8)
在 snapshot_report_js.js:178

我没有发现任何问题,有人可以帮忙吗?

【问题讨论】:

    标签: json tree dojo store dijit.tree


    【解决方案1】:

    乍一看,您的ObjectStoreModel 正在尝试查找树的根对象,并且正如您指定的那样,它应该具有属性id 等于snapshot;您的 JSON 中没有任何内容与该查询匹配。

    其次,JSON 数据应该带来树状结构的内容,而您的 JSON 是非结构化的;请参阅ObjectStoreModel example 树数据的外观。如果您有自定义数据结构,则需要将其转换为通过其模型由树小部件使用。

    【讨论】:

    • 我的问题解决了。
    • 很高兴听到!遵守礼仪并分享您的发现或结束未回答的问题会更好。
    猜你喜欢
    • 2015-10-23
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2013-01-07
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多