当前位置 : 主页 > 网页制作 > Dojo >

dojo tree的操作及实例

来源:互联网 收集:自由互联 发布时间:2021-06-15
这是动态生成的一些插件,这是我项目中的一个页面,其中有dojo tree的展示 (function(){ dojo.provide("spolo.navigator.ugelocal"); dojo.require("dijit.form.Button"); dojo.require("dijit.Menu"); dojo.require("dojo.dat

 


这是动态生成的一些插件,这是我项目中的一个页面,其中有dojo tree的展示 

  1. (function(){ 
  2.  
  3. dojo.provide("spolo.navigator.ugelocal"); 
  4. dojo.require("dijit.form.Button"); 
  5. dojo.require("dijit.Menu"); 
  6. dojo.require("dojo.data.ItemFileReadStore"); 
  7. dojo.require("dijit.Tree"); 
  8. dojo.require("dojo.store.JsonRest"); 
  9. dojo.require("dijit.MenuItem"); 
  10. dojo.require("dijit.form.Form"); 
  11. dojo.require("dijit.Dialog") 
  12. dojo.require("dijit.form.ValidationTextBox"); 
  13. dojo.require("dijit.form.Select"); 
  14. dojo.require("dijit.layout.BorderContainer"); 
  15. dojo.require("dijit.layout.ContentPane"); 
  16. dojo.require("dojo.data.ItemFileReadStore"); 
  17.  
  18.               var usGov = new dojo.store.JsonRest({ 
  19.                                 target:"", 
  20.                                 //判断有没有孩子节点 
  21.                                 mayHaveChildren: function(object){ 
  22.                                    //等到路径path下的节点集合 
  23.                                    var hasNode=Sling.getContent(object.path,1); 
  24.                                    //遍历节点集合 
  25.                                    for(var child in hasNode){ 
  26.                                        var isHave=Sling.getContent(object.path+"/"+child,1); 
  27.                                        var itemnode=isHave.name; 
  28.                                        if(itemnode==object.name){} 
  29.                                        else if(itemnode==undefined){} 
  30.                                        else{ 
  31.                                            return true;//为true,则有孩子节点 
  32.                                        } 
  33.                                    } 
  34.                                    return false; 
  35.                                     
  36.                                 }, 
  37.                                 //得到指定object下孩子节点 
  38.                                 getChildren: function(object, onComplete, onError){ 
  39.                                  
  40.                                             this.get(object.path+".tidy.1.json").then(function(fullObject){ 
  41.                                                 var pNode=object
  42.                                                 //alert(object.children); 
  43.                                                 var path=object.path; 
  44.                                                 var arrayObj=new Array();//声明一个数组用来放孩子节点集合 
  45.                                                  
  46.                                                 var node1=Sling.getContent(path,1); 
  47.                                                     for(var node2 in node1){ 
  48.                                                         var accessDir=path+"/"+node2; 
  49.                                                        var item=Sling.getContent(accessDir,1); 
  50.                                                        //得到item的属性 
  51.                                                        var itemitemnode=item.name; 
  52.                                                        var itemitemFileType=item.fileType; 
  53.                                                        var itemitemContent=item.content; 
  54.                                                        var type=item.type; 
  55.                                                        item.children=new Array(); 
  56.                                                         
  57.                                                        if(itemnode==object.name){} 
  58.                                                        else if(itemnode==undefined){} 
  59.                                                        else{ 
  60.                                                                //声明一个对象,包含各个指定的属性 
  61.                                                                var temp=new Object({ 
  62.                                                                      id:Math.random(),//id属性 
  63.                                                                      name:itemnode,//name名称 
  64.                                                                      type:type, 
  65.                                                                      path:accessDir,//项目路径 
  66.                                                                      fileType:itemFileType,//项目文件类型 
  67.                                                                      content:itemContent,//文件包含的内容 
  68.                                                                      children:item.children, 
  69.                                                                      parentNode:pNode 
  70.                                                                }); 
  71.                                                                 arrayObj.push(temp);//把每个item节点对象放到arrayObj的集合中 
  72.                                                        } 
  73.                                                     } 
  74.                                                     object.children=arrayObj 
  75.                                                     onComplete(object.children); 
  76.                                                 }, onError); 
  77.                                  }, 
  78.                                 //得到更节点 
  79.                                 getRoot: function(onItem, onError){ 
  80.                                     this.get("/content/ugescene.json").then(onItem, onError); 
  81.                                 }, 
  82.                                 //的每一个tree的节点名字,用的是后台数据的name属性 
  83.                                 getLabel: function(object){ 
  84.                                     return object.name; 
  85.                                 }, 
  86.                                 //更改一个tree节点 
  87.                                 put: function(object, options){ 
  88.                                     // fire the onChildrenChange event 
  89.                                     this.onChildrenChange(object,object.children); 
  90.  
  91.                                    // this.getChildren(object, onComplete, onError);   
  92.                                     this.onChange(object); 
  93.                                     // execute the default action 
  94.                                     return dojo.store.JsonRest.prototype.put.apply(this, arguments); 
  95.                                } 
  96.                                 
  97.                         }); 
  98.                  //声明一棵树,场景树用来显示场景的目录结构 
  99.                  var localSceneTree = new dijit.Tree({ 
  100.                     model: usGov, 
  101.                     onClick:function(item,node,evt){ 
  102.                         //根据节点的类型来显示不同的toolbar 
  103.                        if(item.type=="sector"){ 
  104.                           dojo.publish("/toolbar/changed",[{clsid:"spolo.toolbars.sectorToolbar"}]); 
  105.                        }if(item.type=="mesh"){ 
  106.                           dojo.publish("/toolbar/changed",[{clsid:"spolo.toolbars.meshToolbar"}]); 
  107.                        } 
  108.                        window.document.getElementById('ugeScene').contentWindow.getMeshProperty(item.name); 
  109.                         
  110.                     } 
  111.                  }); 
  112.                   
  113.                  localSceneTree.startup(); 
  114.       以上是遍历一个josnrest的tree,他访问的是个json文件,或者说jcr中的一个节点,
  115. 他们都可以以josn的形式来访问。
  116.  
  117.         _getScenen:function(msg){ 
  118.             //alert("message=============="+msg.message); 
  119.                     //得到传递过来的休息 
  120.                     var data=msg.message; 
  121.  
  122.                      var lo = this._context.get("layout"); 
  123.                      var left = lo.getRegion("left"); 
  124.                      //把得到的休息转化成json对象 
  125.                      var rawdata = eval('('+data+')') 
  126.                      //alert(rawdata); 
  127.                      //声明store 
  128.                      var store = new dojo.data.ItemFileReadStore({ 
  129.                         data: { identifier: 'id', label : 'name', items: rawdata } 
  130.                      }); 
  131.                     var treeModel = new dijit.tree.ForestStoreModel({ store: store ,rootId:"laiba",rootLabel:"laiba"}); 
  132.                     //声明一个tree来显示store 
  133.                     var treeControl = new dijit.Tree({ 
  134.                         model: treeModel, 
  135.                         showRoot: true, 
  136.                         onClick:function(item,node,evt){ 
  137.                             //给scene传递休息 
  138.                             //alert(item.name+item.type); 
  139.                             var str=item.name+","+item.type; 
  140.                             if(item.type=='sector'){ 
  141.                                window.document.getElementById('ugeScene').contentWindow.getSectorProperty(str); 
  142.                             }else if(item.type=='mesh'){ 
  143.                                window.document.getElementById('ugeScene').contentWindow.getMeshProperty(str); 
  144.                             } 
  145.                                
  146.                             //dojo.publish("/toolbar/changed",[{clsid:"spolo.toolbars.sectorToolbar"}]); 
  147.                         } 
  148.  
  149.                     }); 
  150.                  left.setContent(""); 
  151.                  //填充右侧toolbar 
  152.                  treeControl.placeAt(left.domNode,'first'); 
  153.                  treeControl.startup(); 
  154.                  //把this对象赋给temp对象,这样为了调函数时this指针发生变化 
  155.                  var temp=this
  156.                  //声明一个button,来测试选中tree中的某一个节点 
  157.                  var btn=new dijit.form.Button({ 
  158.                     label:"expand", 
  159.                     onClick:function(){ 
  160.                             //选中tree中的某一个节点 
  161.                             //定义一个数组 
  162.                             var buildme = []; 
  163.                             var result = temp.recursiveHunt("sectorlist", treeControl.model, buildme, treeControl.model.root); 
  164.                             if(result && result.length > 0){ 
  165.                                 treeControl.set('path', result); 
  166.                             }      
  167.                     } 
  168.                  }); 
  169.                  btn.placeAt(left.domNode,'second'); 
  170.                  btn.startup(); 
  171.                   
  172.         }, 
  173.         recursiveHunt:function(lookfor, model, buildme, item){  
  174.                 var id = model.getIdentity(item);//得到item的id 
  175.                 buildme.push(id);//放到数组中 
  176.                 if(id == lookfor){ 
  177.                     return buildme;//如果id符合条件,返回数组 
  178.                 } 
  179.                 for(var idx in item.children){ 
  180.                     //slice()是用来截取数组中的一部分,用它来复制数组,如果省略了end参数,则切分的数组包含从start开始到数组结束的所有元素。 
  181.                     //现在要用它来复制数组 
  182.                     var buildmebuildmebranch = buildme.slice(0); 
  183.                     //递归 
  184.                     var r = this.recursiveHunt(lookfor, model, buildmebranch, item.children[idx]); 
  185.                     if(r){ return r; } 
  186.                 } 
  187.                 return undefined; 
  188.         }, 
  189. 以上是一个把一json形式的字符串转化为json对象,让后便利的一种方法,还有定义了一个按钮,可以使tree
  190. 中的某一个节点被选中及打开他的父节点(expand one node in a dijit.tree)
上一篇:dojo.Deferred
下一篇:dojo.toJson
网友评论