Dojo读取json生成树

时间:2022-07-24 11:41:31
第一种方式: 
1、 //头部引入dojo包和样式

<style type="text/css">@import "js/dojo-release-1.8.0-src/dijit/themes/tundra/tundra.css";
@import "js/dojo-release-1.8.0-src/dojo/resources/dojo.css";
</style>
<!-- Date: 2012-10-08 -->
<script type="text/javascript" src="js/dojo-release-1.8.0-src/dojo/dojo.js"
djConfig = "isDebug:true,parseOnLoad:true"></script>
<script type="text/javascript" >
dojo.require("dijit.Tree");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dojo.parser");
</script>
2、 /* 创建模拟数据 */
var data = {    identifier: 'id',   
label: 'name',
items: [
{ id:'18',name:'总参谋部',type:'department',children:[{ id:'22',name:'陈炳德', type:'leader' }] },
{ id:'19',name:'总政治部',type:'department',children:[{ id:'23',name:'李继耐', type:'leader' }] },
{ id:'20',name:'总装备部',type:'department',children:[{id:'24',name:'*', type:'leader' }] },
{ id:'21',name:'总后勤部',type:'department',children:[{id:'25',name:'常万全', type:'leader' }] }
]
};
//读取j数据源var continentStore = new dojo.data.ItemFileReadStore({data:data}); //这个是引用XML文件/*  var xmlStore = new dojox.data.XmlStore({  url: "file/country.xml",   label:"name"  });*///创建数据模型var continentModel = new dijit.tree.ForestStoreModel({store:continentStore,query:{"name":"*"},//查询条件rootId:"id", rootLabel:"*",//根结名称childrenAttrs: ["children"]//子节点类型}); //树模型 myTree = new dijit.Tree({model:"continentModel",showRoot:false//是否显示根节点},"contentTree");
3、 <!--页面内容-->
<body class="tundra"><div id = "contentTree"></div></body>
其中的属性就不解释了,参考官网api:http://dojotoolkit.org/api
第二种方式:
<!--获取数据源-->  <div dojoType="dojo.data.ItemFileReadStore" jsId="continentStore" url="json/dept.json"></div>
<!--创建树模型-->
<div dojoType="dijit.tree.ForestStoreModel" jsId="continentModel" store="continentStore"
childrenAttr="children" query="{type:'department'}"rootId="continentRoot" rootLabel="*国防部">
</div>
<!--根据数据模型展示树结构-->
<div dojoType="dijit.Tree" id="mytree" jsId="mytree" model="continentModel" ></div>