我有一个代表菜单项的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时出现错误:&#34; myStore未定义&#34;。
感谢您的帮助
答案 0 :(得分:0)
看起来您正在混合使用旧的dojo / data / store和更新的dojo / store API。 dojo / store apis上没有getValue方法。
这是一个工作版本:
// Code goes here
需要([ &#34;道场/ _base /窗口&#34 ;, &#34;道场/ DOM&#34 ;, &#34;道场/存储/记忆&#34 ;, &#34;的dijit /树/ ObjectStoreModel&#34 ;, &#34;的dijit /树&#34 ;, &#34;道场/ domready中&#34!; ],function(win,dom,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){
// 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();
});