Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript dojo dijit树:如何管理父节点、子节点和叶节点?_Javascript_Node.js_Dojo - Fatal编程技术网

Javascript dojo dijit树:如何管理父节点、子节点和叶节点?

Javascript dojo dijit树:如何管理父节点、子节点和叶节点?,javascript,node.js,dojo,Javascript,Node.js,Dojo,我有一个代表菜单项的dijit树, 有些有子节点,有些是叶节点 我想知道如何用javascript编写,当我有一个带有child的普通节点和一个叶节点时,应该使用哪个属性? 如何写入:在这种情况下使用文件夹图标,在另一种情况下使用叶子图标 这是我的代码: <script> require([ "dojo/_base/window", "dojo/store/Memory", "dijit/tree/ObjectStoreModel", "dijit/Tree",

我有一个代表菜单项的dijit树, 有些有子节点,有些是叶节点

我想知道如何用javascript编写,当我有一个带有child的普通节点和一个叶节点时,应该使用哪个属性? 如何写入:在这种情况下使用文件夹图标,在另一种情况下使用叶子图标

这是我的代码:

<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>

要求([
“dojo/_base/window”、“dojo/store/Memory”,
“dijit/tree/ObjectStoreModel”、“dijit/tree”、“dojo”,
“dojo/domReady!”,“dojo/parser”
],函数(win、内存、ObjectStoreModel、树、dojo){
//创建测试存储,添加ObjectStoreModel所需的getChildren()方法
var myStore=新内存({
数据:[
{id:1,名称:'菜单',url:'http://dojotoolkit.org/api/1.6/dijit.Tree,根:true,目录:true},
{id:2,名称:'Folder1',url:'http://dojotoolkit.org/api/1.6/dijit.Tree,父:1,目录:false},
{id:3,名称:'Leaf1',url:'http://dojotoolkit.org/api/1.6/dijit.Tree.model,父:2},
{id:4,名称:'Leaf2',url:'http://dojotoolkit.org/api/1.6/dijit.tree.ForestStoreModel,父:2},
...
],
getChildren:函数(对象){
返回此.query({parent:object.id});
}
});
//创建模型
var myModel=新的ObjectStoreModel({
商店:myStore,
查询:{root:true}
});
//创建树,指定onClick方法
(新树({
型号:myModel,
getIconClass:函数(项,已打开){
返回myStore.getValue(项,'directory')?(打开?“DijitFolderOpen”:“dijitFolderClosed”):“dijitLeaf”;
},
onClick:函数(项){
//从项目中获取URL,并导航到它
//location.href=item.url;
//parent.getElementById('central').src=item.url;
parent.central.location.href=item.url;
}
})).placeAt(win.body()).startup();
});
但它不起作用。
我尝试使用myStore中第一个和第二个项目的directory属性,但在加载getIconClass时出错:“myStore未定义”


感谢您的帮助

看起来您正在混合使用传统的dojo/data/store和更新的dojo/store API。dojo/store API上没有getValue方法

以下是一个工作版本:

// Code goes here
要求([ “dojo/_基地/窗口”, “dojo/dom”, “dojo/store/Memory”, “dijit/tree/ObjectStoreModel”, “dijit/树”, “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();
});