【问题标题】:dojo dijit tree : how to manage parents, child and leaf nodes?dojo dijit 树:如何管理父节点、子节点和叶节点?
【发布时间】:2015-12-29 06:31:06
【问题描述】:

我有一个代表菜单项的 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 is undefined”。

感谢您的帮助

【问题讨论】:

    标签: javascript node.js dojo


    【解决方案1】:

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

    这是一个工作版本:

    // Code goes here
    

    要求([ "道场/_base/窗口", "道场/dom", “道场/商店/内存”, "dijit/tree/ObjectStoreModel", "dijit/树", “道场/domReady!” ], 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();
    });
    

    http://plnkr.co/edit/11Z20MpsZyShh1E0Ai7k?p=catalogue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      相关资源
      最近更新 更多