js创建树形结构

//makeTree.js

/*function getAllProfiles(){   
 var hql="from ProfilesHBM hbm";
 ProfilesDAO.findWithHQL(hql,gotAllProfiles);
}
function gotAllProfiles(data){
 MakeTree(data);
}
function MakeTree(data){
 var id="";
 var name="";
 var url="";
 var pobj="";
 var pid="";
 var pname="";
 window.tree = new MzTreeView("tree");
 tree.setIconPath("images/tree/");
 for(i=0;i  //alert(data[i].roles.length);
  id=data[i].id;//当前目录id
  name=data[i].profileName;//当前目录名称
  pobj=data[i].parentId;//当前对象的父类对象
  if(isNull(pobj)){
   tree.N["0_"+id] = "ctrl:sel;T:"+name;
  }else{
   tree.N[""+pobj.id+"_"+id+""] = "ctrl:sel;T:"+name+";url:"+url;
  }
  
 }
 tree.wordLine = false;
 tree.setTarget("_top");
 document.getElementById("treeMsg").innerHTML=tree.toString();
}*/
//获取tree
function getTree(profiles,index,imgUrl,ischeckbox,isRadio){
 makeTree(profiles,index,imgUrl,ischeckbox,isRadio);
}
//profile对象
function profileObj(id,pid,name,url,isChecked){
 this.id=id;
 this.pid=pid;
 this.name=name;
 this.url=url;
 this.isChecked=isChecked;
}
//构造tree
function makeTree(profiles,index,imgUrl,ischeckbox,isRadio){
 var id="";
 var name="";
 var url="";
 var pid="";
 var isC="";
 if(isNull(index))
  index=getDefaultIndex(profiles);
  window.tree = new MzTreeView("tree");
  tree.setIconPath(imgUrl);
  //alert(profiles.length);
  for(i=0;i   var profile=profiles[i];
   id=profile.id;
   name=profile.name;
   url=profile.url;
   pid=profile.pid;
   isC=profile.isChecked;
   //alert(pid+name);
   if(index=='all'){
    if(isNull(pid)){
     tree.N["0_"+id] = "ctrl:sel;T:"+name+";isRadio:"+isRadio+";ischeckbox:"+ischeckbox+";checked:"+isC+"";
    }else{
     //alert(pid+"_"+id);
     tree.N[pid+"_"+id] = "ctrl:sel;T:"+name+";isRadio:"+isRadio+";ischeckbox:"+ischeckbox+";checked:"+isC+"";
    }
   }else{
    if(pid==index || id==index){
     //alert(id);
     if(isNull(pid)){
      tree.N["0_"+id] = "ctrl:sel;T:"+name+";ischeckbox:"+ischeckbox+"";
     }else{
      //alert(pid+"_"+id);
      tree.N[pid+"_"+id] = "ctrl:sel;T:"+name+";url:"+url+";isRadio:"+isRadio+";ischeckbox:"+ischeckbox+"";
     }
    }
   }
  }
  tree.wordLine = false;
  tree.setTarget("content");
  document.getElementById("treeMsg").innerHTML=tree.toString();
  tree.expandAll();
}
//获取默认的index
function getDefaultIndex(profiles){
 var index=1;
 for(i=0;i  var obj=profiles[i];
  if(!isNull(obj.pid)){
   index=obj.pid;
   break;
  }
 }
 return index;
}

 

//MzTreeView.js

/*---------------------------------------------------------------------------*\
|  Subject:       Web TreeView Class                                          |
|  Version:       1.0                                                         |
|  Author:        黄方荣【meizz】【梅花雪】                                   |
|  FileName:      MzTreeView.js                                               |
|  Created:       2004-10-18                                                  |
|  LastModified:  2005-03-10                                                  |
|  Download:      http://www.meizz.com/Web/Download/MzTreeView10.rar          |
|  Explain:       http://www.meizz.com/Web/Article.asp?id=436                 |
|  Demo:          http://www.meizz.com/Web/Demo/MzTreeView10.htm              |
|                                                                             |
|                 You may use this code on your item                          |
|                 this entire copyright notice appears unchanged              |
|                 and you clearly display a link to http://www.meizz.com/     |
|                                                                             |
|-----------------------------------------------------------------------------|
|  MSN: [email protected]   QQ: 112889082   http://www.meizz.com                |
|  CSDN Community ID: meizz      Copyright (c) 2004-2005 meizz                |
\*---------------------------------------------------------------------------*/

//Version: 1.2
//20:31 2007-8-25 http://www.lxasp.com
//为了在大量数据时节省文本大小,修改了接口(请注意大小写):
//
//原来: nodes["pnt_cur"]="text:结点文字;method:js函数()";
//
//改为: N["pnt_cur"]="T:结点文字;C:js函数()";

//MzTreeView1.0网页树类, 在实例化的时候请把实例名作参数传递进来
function MzTreeView(Tname)
{
  if(typeof(Tname) != "string" || Tname == "")
    throw(new Error(-1, '创建类实例的时候请把类实例的引用变量名传递进来!'));
 
  //【property】
  this.url      = "#";
  this.target   = "_self";
  this.name     = Tname;
  this.wordLine = false;
  this.currentNode = null;
  this.useArrow = true;
  this.N = {};
  this.node  = {};
  this.names = "";
  this._d    = "\x0f";
  this.index = 0;
  this.divider   = "_";
  this.node["0"] =
  {
    "id": "0",
    "path": "0",
    "isLoad": false,
    "childNodes": [],
    "childAppend": "",
    "sourceIndex": "0"
  };

  this.colors   =
  {
    "highLight" : "#0A246A",
    "highLightText" : "#FFFFFF",
    "mouseOverBgColor" : "#D4D0C8"
  };
  this.icons    = {
    L0        : 'L0.gif',  //┏
    L1        : 'L1.gif',  //┣
    L2        : 'L2.gif',  //┗
    L3        : 'L3.gif',  //━
    L4        : 'L4.gif',  //┃
    PM0       : 'P0.gif',  //+┏
    PM1       : 'P1.gif',  //+┣
    PM2       : 'P2.gif',  //+┗
    PM3       : 'P3.gif',  //+━
    empty     : 'L5.gif',     //空白图
    root      : 'root.gif',   //缺省的根节点图标
    folder    : 'folder.gif', //缺省的文件夹图标
    file      : 'file.gif',   //缺省的文件图标
    exit      : 'exit.gif'
  };
  this.iconsExpand = {  //存放节点图片在展开时的对应图片
    PM0       : 'M0.gif',     //-┏
    PM1       : 'M1.gif',     //-┣
    PM2       : 'M2.gif',     //-┗
    PM3       : 'M3.gif',     //-━
    folder    : 'folder.gif',

    exit      : 'exit.gif'
  };

  //扩展 document.getElementById(id) 多浏览器兼容性
  //id 要查找的对象 id
  this.getElementById = function(id)
  {
    if (typeof(id) != "string" || id == "") return null;
    if (document.getElementById) return document.getElementById(id);
    if (document.all) return document.all(id);
    try {return eval(id);} catch(e){ return null;}
  };

  //MzTreeView 初始化入口函数
  this.toString = function()
  {
    this.browserCheck();
    this.dataFormat();
    //this.setStyle();// modify by fengxin 2008-04-03
    this.load("0");
    var rootCN = this.node["0"].childNodes;
    var str = "";
    if(rootCN.length>0)
    {
      this.node["0"].hasChild = true;
      for(var i=0; i        str += this.nodeToHTML(rootCN[i], i==rootCN.length-1);
      setTimeout(this.name +".expand('"+ rootCN[0].id +"', true); "+
        this.name +".focusClientNode('"+ rootCN[0].id +"'); "+ this.name +".atRootIsEmpty()",10);
    }

    if (this.useArrow)  //使用方向键控制跳转到上级下级父级子级节点
    {
      if (document.attachEvent)
          document.attachEvent("onkeydown", this.onkeydown);
      else if (document.addEventListener)
          document.addEventListener('keydown', this.onkeydown, false);
    }
    return "

      "οnclick='"+ this.name +".clickHandle(event)' "+
      "οndblclick='"+ this.name +".dblClickHandle(event)' "+
      ">"+ str +"
";
  };

  this.οnkeydοwn= function(e)
  {
    e = window.event || e; var key = e.keyCode || e.which;
    switch(key)
    {
      case 37 : eval(Tname).upperNode(); break;  //Arrow left, shrink child node
      case 38 : eval(Tname).pervNode();  break;  //Arrow up
      case 39 : eval(Tname).lowerNode(); break;  //Arrow right, expand child node
      case 40 : eval(Tname).nextNode();  break;  //Arrow down
    }
  };
};

//浏览器类型及版本检测
MzTreeView.prototype.browserCheck = function()
{
  var ua = window.navigator.userAgent.toLowerCase(), bname;
  if(/msie/i.test(ua))
  {
    this.navigator = /opera/i.test(ua) ? "opera" : "";
    if(!this.navigator) this.navigator = "msie";
  }
  else if(/gecko/i.test(ua))
  {
    var vendor = window.navigator.vendor.toLowerCase();
    if(vendor == "firefox") this.navigator = "firefox";
    else if(vendor == "netscape") this.navigator = "netscape";
    else if(vendor == "") this.navigator = "mozilla";
  }
  else this.navigator = "msie";
  if(window.opera) this.wordLine = false;
};

//给 TreeView 树加上样式设置
MzTreeView.prototype.setStyle = function()
{
  /*
    width: 16px; \
    height: 16px; \
    width: 20px; \
    height: 20px; \
  */
  var style = "