我正在尝试以编程方式从json文件创建dojo树。问题是我的json对象引用了他们的父母而不是像某些例子中的孩子。 不幸的是我的输出看起来像这样:
我有以下javascript代码:
<script type="text/javascript">
require(["dijit/Tree", "dojo/data/ItemFileReadStore", "dijit/tree/ForestStoreModel", "dijit/tree/ObjectStoreModel", "dojo/domReady!"],
function(Tree, ItemFileReadStore, ObjectStoreModel, ForestStoreModel){
var store = new ItemFileReadStore({
url: "/_data/test.json",
getChildren: function(object){
return this.query({F_TopLevelCategoryID: object.P_CategoryID});
}
});
var myModel = new ObjectStoreModel({
store: store,
labelAttr:'CategoryName',
query: {"P_CategoryID": 0}
});
var myTree = new Tree({
model: myModel
}, "treeOne");
myTree.startup();
});
</script>
json文件如下所示:
{ "identifier" : "P_CategoryID",
"label" : "CategoryName",
"items" : [ { "CategoryName" : "Category 1",
"F_TopLevelCategoryID" : 0,
"P_CategoryID" : 1
},
{ "CategoryName" : "Category 2",
"F_TopLevelCategoryID" : 1,
"P_CategoryID" : 2
},
{ "CategoryName" : "Category 3",
"F_TopLevelCategoryID" : 1,
"P_CategoryID" : 3
},
{ "CategoryName" : "Category 4",
"F_TopLevelCategoryID" : 1,
"P_CategoryID" : 4
},
{ "CategoryName" : "Category 5",
"F_TopLevelCategoryID" : 3,
"P_CategoryID" : 5
},
{ "CategoryName" : "Category 6",
"F_TopLevelCategoryID" : 4,
"P_CategoryID" : 6
},
{ "CategoryName" : "Category 7",
"F_TopLevelCategoryID" : 4,
"P_CategoryID" : 7
},
{ "CategoryName" : "Category 8",
"F_TopLevelCategoryID" : 0,
"P_CategoryID" : 8
},
{ "CategoryName" : "Top Level Category",
"P_CategoryID" : 0
}
]
}
问题出在哪里?
答案 0 :(得分:1)
我认为你得到了部分结果是侥幸。
您在需要块中调换了forestStoreModel和objectStoreModel,并且引用父项就可以了!
此外,使用dojo / store而不是ItemFileReadStore,因为后者已弃用。在Memory:
上使用data参数var store = new Memory({
data: testJson,
getChildren: ...
});
使用dojo / store / Memory:https://jsfiddle.net/yubp45sa/
,在此查看我的工作小提琴您可以使用dojo / request将数据存入内存存储:
request("testData.json").then(...);
http://dojotoolkit.org/reference-guide/1.10/dojo/request.html
我之前有另一个请求示例,它通过自定义nodeJS路由(返回JSON)通过GET请求数据:
request.get("/configInfo", {
handleAs: "json"
}).then(...);