<!DOCTYPE html> <html > <head> <link rel="stylesheet" href="../_static/js/dojo/../dijit/themes/claro/claro.css"> <script>dojoConfig = {async: true}</script>
<script src='../_static/js/dojo/dojo.js'></script><script>require([
"dojo/ready", "dojo/_base/window", "dojo/store/Memory", "dijit/tree/ObjectStoreModel", "dijit/Tree" ], function(ready, win, Memory, ObjectStoreModel, Tree){ // Create test store, adding the getChildren() method required by ObjectStoreModel var myStore = new Memory({ data: [ { id: 'world', name:'The earth', type:'planet', population: '6 billion'}, { id: 'AF', name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km', timezone: '-1 UTC to +4 UTC', parent: 'world'}, { id: 'EG', name:'Egypt', type:'country', parent: 'AF' }, { id: 'KE', name:'Kenya', type:'country', parent: 'AF' }, { id: 'Nairobi', name:'Nairobi', type:'city', parent: 'KE' }, { id: 'Mombasa', name:'Mombasa', type:'city', parent: 'KE' }, { id: 'SD', name:'Sudan', type:'country', parent: 'AF' }, { id: 'Khartoum', name:'Khartoum', type:'city', parent: 'SD' }, { id: 'AS', name:'Asia', type:'continent', parent: 'world' }, ], //方法重写,有parent和id来确定children数据 getChildren: function(object){ return this.query({parent: object.id}); } }); // 创建tree需要的核心model,其中重要的为store属性 //query相当于确定tree中的root(根节点) var myModel = new ObjectStoreModel({ store: myStore, query: {id: 'world'} }); //在页面加载完,触发此方法 ready(function(){ var tree = new Tree({ model: myModel }); //将创建好的tree节点放到body处。 tree.placeAt(win.body()); //渲染tree tree.startup(); }); });</script> </head> <body class="claro"> </body> </html>
有兴趣的同学,可以去官网找有关dijit.tree.ObjectStoreModel的资料。多探讨,接下来有时间继续和大家学习tree相关的高级应用。