当前位置: 代码迷 >> Web前端 >> Tree_三
  详细解决方案

Tree_三

热度:427   发布时间:2013-12-11 16:44:13.0
Tree_3
<div dojoType="dojo.data.ItemFileWriteStore" jsId="myStore" id="treeDemo"></div>  <!-- 设置数据源 -->
	<div dojoType="dijit.tree.TreeStoreModel" jsId="catModel" store="myStore" query="{id: '0'}"></div><!-- 设置树的缓存模型 -->
	<div id="collectionsTree" data-dojo-type="dijit.Tree" data-dojo-props='"class":"container", model:catModel,......></div><!-- 引用缓存模型 -->


dojo.data.ItemFileWriteStore是一个读文件的类, 但是可以设置他的data属性效果是一样的
//这个是仿的data数据
	var itemsArr = [{
	                  id : '0',       
	                name : '李英夫',
			  department : 'w3',
			        root : true,
				   level : 1,
			    children : [{_reference: '1'}, {_reference: '2'},{_reference: '3'}]
	               },{ 
				      id : '1', 
				    name : '类别1',
		      department : '部门甲',
					level : 2,
			    children : [{_reference: '4'},{_reference: '5'}]
				   },{ 
				      id : '4',
					name : '路人乙',
			  department : '部门乙',
				level : 3
				   },{ 
				      id : '5',
					name : '路人丙',
			  department : '部门丙',
			  level : 3
			       },{ 
				      id : '2', 
					name : '路人丁',
			  department : '部门丁',
					 level : 3
			       },{ 
				      id : '3',
					name : '路人戊',
			  department : '部门戊',
					 level : 3
			       }];
    

   
    //将数据变成字符串
    var treeData = function(items){
	    this._identifier = 'id';
		this._label = 'name';
		this._items = items;
		this.getData = function(){
		    var s = "{";
			    s += "identifier:'" + this._identifier + "',";
				s += "label:'" + this._label + "',";
				s += "items:[";
			for(var i = 0, len = this._items.length; i < len; i++){
			    s += "{id:'" + this._items[i].id  + "',";
				s += "name:'" + this._items[i].name + "',";
				s += "department:'" + this._items[i].department + "',";
				s += "level:" + this._items[i].level + "";
				if(!!this._items[i].children){
				   s += ",children : [";
				   var cs = this._items[i].children;
				   for(var j = 0, jLen = cs.length; j < jLen; j++){
				       s += "{_reference:'" + cs[j]._reference + (j == (jLen-1) ? "'}]" : "'},");
				   } 
				}
				s += (i == (len-1) ? "}" : "},");
			}
			s += "]}";
			return s;
		};
	}
    

        //添加到dojo.data.ItemFileWriteStore所声明的标签中.
	document.getElementById("treeDemo").setAttribute("data", new treeData(itemsArr).getData());
 
  相关解决方案