[英]dojo dijit tree : how to manage parents, child and leaf nodes?
我有一棵表示菜单项的dijit树,有些有孩子,有些是叶子节点。
我想知道如何用javascript编写此代码,当我有一个带有子节点和一个叶节点的普通节点时要使用哪个属性? 如何编写:在这种情况下,请使用文件夹图标,在其他情况下使用叶图标。
有我的代码:
<script>
require([
"dojo/_base/window", "dojo/store/Memory",
"dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo",
"dojo/domReady!", "dojo/parser"
], function(win, Memory, ObjectStoreModel, Tree, dojo){
// Create test store, adding the getChildren() method required by ObjectStoreModel
var myStore = new Memory({
data: [
{ id: 1, name: 'Menu', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', root: true, directory: true },
{ id: 2, name: 'Folder1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', parent: 1, directory: false},
{ id: 3, name: 'Leaf1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree.model', parent: 2 },
{ id: 4, name: 'Leaf2', url: 'http://dojotoolkit.org/api/1.6/dijit.tree.ForestStoreModel', parent: 2 },
...
],
getChildren: function(object){
return this.query({parent: object.id});
}
});
// Create the model
var myModel = new ObjectStoreModel({
store: myStore,
query: {root: true}
});
// Create the Tree, specifying an onClick method
(new Tree({
model: myModel,
getIconClass:function(item, opened){
return myStore.getValue(item, 'directory') ? (opened ? "dijitFolderOpened" : "dijitFolderClosed") : "dijitLeaf";
},
onClick: function(item){
// Get the URL from the item, and navigate to it
// location.href = item.url;
//parent.getElementById('central').src = item.url;
parent.central.location.href = item.url;
}
})).placeAt(win.body()).startup();
});
</script>
但这是行不通的。
我尝试使用myStore中第一项和第二项的目录属性,但是在加载getIconClass时出现错误:“ myStore未定义”。
感谢帮助
看起来您正在混用旧版dojo / data / store和较新的dojo / store API。 dojo / store api上没有getValue方法。
这是一个工作版本:
// Code goes here
require([[dojo / _base / window,“ dojo / dom”,“ dojo / store / Memory”,“ dijit / tree / ObjectStoreModel”,“ dijit / Tree”,“ dojo / domReady!”],函数(win ,dom,内存,ObjectStoreModel,树,dojo){
// Create test store, adding the getChildren() method required by ObjectStoreModel
var myStore = new Memory({
data: [
{ id: 1, name: 'Menu', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', root: true, directory: true },
{ id: 2, name: 'Folder1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree', parent: 1, directory: false},
{ id: 3, name: 'Leaf1', url: 'http://dojotoolkit.org/api/1.6/dijit.Tree.model', parent: 2 },
{ id: 4, name: 'Leaf2', url: 'http://dojotoolkit.org/api/1.6/dijit.tree.ForestStoreModel', parent: 2 }
],
getChildren: function(object){
return this.query({parent: object.id});
}
});
// Create the model
var myModel = new ObjectStoreModel({
store: myStore,
query: {root: true}
});
// Create the Tree, specifying an onClick method
(new Tree({
model: myModel,
getIconClass:function(item, opened){
// You already have the item. No use to try and refetch it from the store
return item.directory ? (opened ? "dijitFolderOpened" : "dijitFolderClosed") : "dijitLeaf";
},
onClick: function(item){
// Get the URL from the item, and navigate to it
parent.central.location.href = item.url;
}
})).placeAt(win.body()).startup();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.