js写的TreeView[支持动态添加,删除,更改,ICON]

我不是很喜欢asp.net里面的TreeView,好象代码太多..

现在用javascript来些个和大家分享下,大概如下图

使用改类的代码如下:

js类:

function Node(text_,value_)
{
 /*
 作者:Mark
 邮箱:benymark@gmail.com
 blog:Http://blog.csdn.net/benymark
 节点类:记录个节点的信息
 */
   this.text=text_;
   this.value=value_;
   this.imageExpand="";//展开后Icon图片
   this.imageColl="";  //折叠后Icon图片
   this.guid=newGuid();
   this.isExpand=true;
   this.level=0;
   this.isLast=true;
   this.parentNode=null;
   this.children=new Array();
   this.addChildNode=function(node){
       node.parentNode=this;
       node.level=this.level+1;
       this.children.push(node);
   };
   this.deleteChildByGuid=function(guid_){
      for(var i=0;i<this.children.length;i++)
      {
         if(this.children[i].guid==guid_)
         {
           return this.children.splice(i,1);
         }
      }
     return null;
   };
   function newGuid()
          {
             var guid = "";
             for (var i = 1; i <= 32; i++)
             {
               var n = Math.floor(Math.random()*16.0).toString(16);
               guid +=   n;
               if((i==8)||(i==12)||(i==16)||(i==20))
               guid += "";
            }
            return guid;   
         }
  
}

function TreeObj(domId_,objname_)
{
    /*
 作者:Mark
 邮箱:benymark@gmail.com
 blog:Http://blog.csdn.net/benymark
 树类:操作个节点成树
 */
   this.nodes=new Array();
   this.objname=objname_;
   this.imageExpand=null;
   this.imageColl=null;
   this.imageEnd=null;
   this.rootcss="";
   this.parentcss="";
   this.leafcss="";
   this.hovicss="";
   this.isShowLine=true;
   this.domId=domId_;
   this.folderpath="..";
   this.onClickNode=null;
   function expand(node,isEx)
   {
         for(var i=0;i<node.length;i++)
         {
           node[i].isExpand=isEx;
           expand(node[i].children,isEx);
         }
   }
   function expandRev(node){
     for(var i=0;i<node.length;i++){
       node[i].isExpand=!node[i].isExpand;
       expandRev(node[i].children);
     }
   }
   this.expandReverse=function(){
        expandRev(this.nodes);
        this.render();
   };
   this.expandAll=function(isEx){
    expand(this.nodes,isEx);
       this.render();
   };
   this.addRootNode=function(node){
   this.nodes.push(node);
   };
   this.findByGuid=function(guid){
     for(var i=0;i<this.nodes.length;i++)
     {
       if(this.nodes[i].guid==guid)
       {
         return this.nodes[i];
       }
       else
       {
        var tmp=FindCircle(this.nodes[i].children,guid);
        if(tmp!=null) return tmp;
       }
     }
     return null;
   };
   this.deleteByGuid=function(guid){
     var node_tmp=this.findByGuid(guid);
     if(node_tmp!=null)
     {
       if(node_tmp.parentNode!=null && node_tmp.parentNode!='undefined'){
           node_tmp.parentNode.deleteChildByGuid(node_tmp.guid);
       }else{
           for(var i=0;i<this.nodes.length;i++){
              if(this.nodes[i].guid==node_tmp.guid){
              this.nodes.splice(i,1);
              }
           }
       }
       this.render();
       return node_tmp;
     }else return null;
  
   };
   function FindCircle(nodes,guid){
        //var nodes=new Node();
        for(var i=0;i<nodes.length;i++)
        {
              if(nodes[i].guid==guid)
              {
                return nodes[i]
              }else {
               var tmp= FindCircle(nodes[i].children,guid);
               if(tmp!=null) return tmp;
              }
        }
        return null;
   }
   this.ParenIsLast=function(node_,level_)
   {
     if(node_==null )
     {return false;}
     else if(node_.level==level_ )
     {
      return node_.isLast;
     }
     else
     {
      return this.ParenIsLast(node_.parentNode,level_);
     }

   };
   this.rootShowOrHide=function(guid,isImage){
    var div=document.getElementById("div_"+guid);
     var img=document.getElementById("img_"+guid);
     var text_obj=document.getElementById("text_"+guid);
     var icon_img=document.getElementById("icon_"+guid);
     var node_=new Node();
     var i;
     for(i=0;i<this.nodes.length;i++)
     {
       if(this.nodes[i].guid==guid)
       {
         node_=this.nodes[i];
         break;
       }
     }
     try{
          if(this.onClickNode!=null && !isImage){
            if(this.onClickNode(node_)) return;
          }
     }catch(e){ alert(e);} 
     if(node_.children.length==0){  return;}
    
     if(div.style.display=='none'){
       div.style.display='block';
       node_.isExpand=true;
     
       if(icon_img!=null){icon_img.src=node_.imageExpand;}
      
           if(img==null) return ;
           if(this.nodes.length==1){/*只有一个节点*/
              img.src=this.folderpath+"/MarkTree/image/root_onlye.gif";
           }else if(node_.isLast){/*节点在结束位置*/
              img.src=this.folderpath+"/MarkTree/image/root_ee.gif";
           }else if(i==0){/*节点在开始位置*/
              img.src=this.folderpath+"/MarkTree/image/root_c.gif";
           }else{ /*节点在中间位置*/
              img.src=this.folderpath+"/MarkTree/image/root_me.gif";
           }
      
     }else{
    
       div.style.display='none';
       node_.isExpand=false;
       if(icon_img!=null){icon_img.src= node_.imageColl;}
      
      
      
       if(img==null) return ;
       if(this.nodes.length==1){/*只有一个节点*/
          img.src=this.folderpath+"/MarkTree/image/root_onlyc.gif";
       }else if(node_.isLast){/*节点在结束位置*/
          img.src=this.folderpath+"/MarkTree/image/root_ec.gif";
       }else if(i==0){/*节点在开始位置*/
          img.src=this.folderpath+"/MarkTree/image/root_e.gif";
       }else{/*节点在中间位置*/
          img.src=this.folderpath+"/MarkTree/image/root_mc.gif";
       }
     }
   };
   this.showOrHide=function(guid,obj){
     var div=document.getElementById("div_"+guid);
     var text_obj=document.getElementById("text_"+guid);
     var node_=this.findByGuid(guid);
     try{
         if(this.onClickNode!=null && !obj.src){
            if(this.onClickNode(node_)) return;
         }}catch(e1){}
     try{  
        
         if(obj==false) return ;
        
         var img=document.getElementById("img_"+guid);
         var icon_img=document.getElementById("icon_"+guid);
        
         if(div.style.display=='none')
         {
               node_.isExpand=true;
               div.style.display='block';
               if(icon_img!=null){icon_img.src=node_.imageExpand;}
              
               if(node_.isLast){
                 img.src=this.folderpath+"/MarkTree/image/root_ee.gif";
               }else{
                 img.src=this.folderpath+"/MarkTree/image/root_me.gif";
               }
         }
         else
         {
          
           node_.isExpand=false;
           div.style.display='none';
           if(icon_img!=null){icon_img.src=node_.imageColl;} 
            
            
           if(node_.isLast){
           img.src=this.folderpath+"/MarkTree/image/root_ec.gif";
           }else{
           img.src=this.folderpath+"/MarkTree/image/root_mc.gif";
           }
         }
     }catch(e){}
   };
   this.mouseOut=function(obj_e,guid_,classname_){
     obj_e.className=classname_;
   };
   this.mouseOver=function(obj_e,guid_){
    if(this.hovicss!=null && this.hovicss.length>0)
      obj_e.className=this.hovicss;
   };
   function circle(node_,thisInstance)
   {
        var html="";

        for(var i=0;i<node_.children.length;i++)
        {
             var node_tmp;
             var icon_image="";
             node_.children[i].isLast=(i==node_.children.length-1);
             node_tmp=node_.children[i];
            
             var guid=node_tmp.guid;
             var mouseOverStr=" οnmοuseοver=/""+thisInstance.objname+".mouseOver(this,'"+guid+"');/" ";
             var clickevent=" οnclick=/""+thisInstance.objname+".showOrHide('"+ guid+"',this);/" ";
              
               if(node_tmp.imageExpand.length>0&& node_tmp.imageColl.length>0){
                   icon_image=(node_tmp.isExpand)?node_tmp.imageExpand:node_tmp.imageColl;
                   icon_image="<img src='"+icon_image+"' style='cursor:pointer;'  id='icon_"+guid+"' />";
               }
               
            
             html+="<table border=0 class=f1  cellpadding=0 cellspacing=0><tr>";
             for(var j=0;j<node_tmp.level;j++)/*梯格*/
             {
               var line_imag=((thisInstance.ParenIsLast(node_,j)|| !thisInstance.isShowLine)?" ":"<img src='"+thisInstance.folderpath+"/MarkTree/image/line.gif'  />");
               html+="<td width='18' align='right'>"+line_imag+"</td>";
             }
            

             if(node_tmp.children.length==0)/*没有子节点*/
             {
                   var mouseOutStr=" οnmοuseοut=/""+thisInstance.objname+".mouseOut(this,'"+guid+"','"+thisInstance.leafcss+"');/"  ";
                   var point_image="<img src='"+thisInstance.folderpath+"/MarkTree/image/"
       +((!node_tmp.isLast)?"sub_m.gif":"sub_e.gif")+"' id=/"img_"+guid+"/" />";
                   if(!thisInstance.isShowLine) point_image="";
                  
                html+="<td width='18' align='right'>"+ point_image+"</td>";
                html+="<td class='"+thisInstance.leafcss+"' id=/"text_"+guid+"/" "+mouseOverStr+mouseOutStr
                +" οnclick=/""+thisInstance.objname+".showOrHide('"+ guid+"',false);/" >"+icon_image+node_tmp.text+"</td></tr></table>";
                html+="<div id='div_"+guid+"' ";
                html+=" style=/"display:none;/"></div>";
             }
             else /*有子节点*/
             {
                var image_name="";
                var mouseOutStr=" οnmοuseοut=/""+thisInstance.objname+".mouseOut(this,'"+guid+"','"+thisInstance.parentcss+"');/"  ";
                if(i<node_.children.length-1)
                {
                 image_name=(node_tmp.isExpand)?"root_me.gif":"root_mc.gif";
                }else{
                 image_name=(node_tmp.isExpand)?"root_ee.gif":"root_ec.gif";
                }
                var point_image="<img src='"+thisInstance.folderpath+"/MarkTree/image/"+image_name+"' "+clickevent
       +"  id=/"img_"+guid+"/" style='cursor:pointer;' />";
              
                   if(!thisInstance.isShowLine) point_image="";
               
                html +="<td width='18' align='right'>"+point_image+"</td>";
                html +="</td><td class='"+thisInstance.parentcss+"' id=/"text_"+guid+"/" class='"+
                thisInstance.parentcss+"' style='cursor:pointer;' "+clickevent
                +"  "+mouseOutStr+mouseOverStr+" >"+icon_image+node_tmp.text+"</td></tr></table>";
                html +="<div id='div_"+guid+"' ";
                html +=" style=/"display:"+((node_tmp.isExpand)?"block":"none")+";/">";
                html +=circle(node_tmp,thisInstance);
                html +="</div>";
             }
        }
        return html;
   }

   this.getHtml=function(){
          var html="";
       for(var i=0;i<this.nodes.length;i++)
       {
         this.nodes[i].isLast=(i==this.nodes.length-1);
         var node_tmp=this.nodes[i];
         var image="";
         var actionfun=" οnclick=/""+this.objname+".rootShowOrHide('"+node_tmp.guid+"',true);/"";
         var guid=node_tmp.guid;
         var img_tmp;
         var icon_img="";
         //node_tmp=new Node();
         if(node_tmp.imageColl.length>0 && node_tmp.imageExpand.length>0)
         {
          icon_img="<img src=/""+((node_tmp.isExpand)?node_tmp.imageExpand:node_tmp.imageColl)+"/" id='icon_"+guid+"' />";
         }
        
         if(this.nodes.length==1)/* 只有唯一一项 */
         {
               img_tmp=((node_tmp.isExpand)?("root_onlye.gif"):("root_onlyc.gif"));
               if(this.nodes[0].children.length==0){img_tmp="root_only.gif"}
              
         }else if(i==0){/*节点在开始位置 */
               img_tmp=((node_tmp.isExpand)?"root_c.gif":"root_e.gif");
               if(node_tmp.children.length==0){img_tmp="root_top.gif";}
              
         }else if(!node_tmp.isLast){/*节点在结束位置 */
               img_tmp=(node_tmp.isExpand)?"root_me.gif":"root_mc.gif";
               if(node_tmp.children.length==0){img_tmp="sub_m.gif";}
              
         }else{/* 节点在中间位置 */

                img_tmp=((node_tmp.isExpand)?"root_ee.gif":"root_ec.gif");
                 if(node_tmp.children.length==0){img_tmp=("sub_e.gif");}            
         }
         image="<img src='"+this.folderpath+"/MarkTree/image/"+img_tmp+"' "+actionfun+"  id='img_"+guid+"' />";
         if(!this.isShowLine) image="";
        
        
         html+="<table border=0 cellpadding=0 cellspacing=0 class=f1><tr><td width='18' align='left' style='cursor:pointer;'>"
         +image+"</td><td οnclick=/""+this.objname+".rootShowOrHide('"+guid+"',false);/" style='cursor:pointer;' class='"
         +this.rootcss+"'>"+icon_img+node_tmp.text+"</td></tr></table>";
         html+="<div id='div_"+guid+"' style=/"display:"+((node_tmp.isExpand)?("block"):("none"))+"/">";
         html+= circle(node_tmp,this);
         html+="</div>";
       }
       return html;
   };
   this.render=function(){
          var html_=this.getHtml();
       document.getElementById(this.domId+"").innerHTML=html_;
       return html_;
   };
  
}

 

//==============================================================

 

 

 

<div id="TreeId">Tree Div</div>

 

var myTree=new TreeObj("TreeId","myTree");//TreeSpan:Dom元素id,myTree:把一定要和变量名一致的字符串
//myTree.isShowLine=false;//当不想显示引导线时候设置该值为false
myTree.folderpath="../TreeDemo";//当你把改目录改变后可以修改这里


myTree.hovicss="hovfont";//当鼠标移到上面时候的样式
myTree.rootcss="rootfont";//根节点的样式
myTree.parentcss="parentfont";//父节点的样式
myTree.leafcss="leaffont";//子节点的样式


//当点某节点后的事件响应
myTree.onClickNode=function(node){
document.getElementById("TextBox_guid").value=node.guid;
document.getElementById("Node_Text").innerHTML="["+node.text+"/"+node.value+"]";
//return true;//要让不这点则返回true就可以
};


//==================添加节点=====================================================
var rootNode=new Node("开发文档","开发文档");// 添加根节点
    rootNode.imageColl="folder2.gif";//折叠后的icon
    rootNode.imageExpand="folder.gif";//展开后的icon
    myTree.addRootNode(rootNode);
     
var subNode1=new Node("j2me手机开发","我是j2me手机开发的Value");//2级节点添加
    subNode1.imageColl="pen.gif";
    subNode1.imageExpand="delete2.gif";
    rootNode.addChildNode(subNode1);//该节点添加到“开发文档”的根节点下面 

   
var subNode_2=new Node("SMS","SMS_VALUE");//3级节点添加
    subNode1.addChildNode(subNode_2);//把3级节点添加到2级节点下面

    subNode_2=new Node("IM socket","IM Socket Value");
    subNode1.addChildNode(subNode_2);//把3级节点添加到2级节点下面
   
   
    subNode1=new Node(".NET开发",".NET开发 ^_^");//2级节点添加
    subNode1.isExpand=false;//让节点默认时候是折叠的
    subNode1.imageColl="folder2.gif";
    subNode1.imageExpand="folder.gif";
    rootNode.addChildNode(subNode1);
   
   
    subNode_2=new Node("ASP.NET","ASP.NET VALUE");//3级节点添加
    subNode_2.imageColl="pen.gif";
    subNode_2.imageExpand="pen.gif";
    subNode1.addChildNode(subNode_2);
   
    subNode_2=new Node("NHibernate","hibernate");
    subNode_2.imageColl="pen.gif";
    subNode_2.imageExpand="pen.gif";
    subNode1.addChildNode(subNode_2);
    subNode_2=new Node("WCF","WCF");
    subNode_2.imageColl="delete.gif";
    subNode_2.imageExpand="delete.gif";
    subNode1.addChildNode(subNode_2);
    //=======================================
    rootNode=new Node("这根节点未添加子项","none");
    myTree.addRootNode(rootNode);
    //========================================
    rootNode=new Node("我的个人文件","My Document");// 添加根节点
    //rootNode.isExpand=false;
    myTree.addRootNode(rootNode);  
   
    subNode1=new Node("我媒体","my media");//2级节点
    rootNode.addChildNode(subNode1);
   
    subNode_2=new Node("我的电影","MY MOVIES");
    subNode1.addChildNode(subNode_2);
    subNode_2=new Node("我的歌曲","My Musics");
    subNode1.addChildNode(subNode_2);
    subNode_2=new Node("我的相片","My Pictures");
   
    subNode1=new Node("我的秘密文件","Secury");//2级节点
    rootNode.addChildNode(subNode1);
    subNode_2=new Node("我的简历","about me");
    subNode1.addChildNode(subNode_2);
    subNode_2=new Node("我的情信","^_^..");
    subNode1.addChildNode(subNode_2);
    subNode_2=new Node("<a href=/"javascript:void window.open('http://blog.csdn.net/benymark');/" targe='_blank'>我的Blog</a>"
    ,"http://blog.csdn.net/benymark");
    subNode1.addChildNode(subNode_2);
   

//=====================================================
  myTree.render();//把节点信息更新到Tree上   

 

 

 

具体请看代码吧...下载Demo.rar

备注:转载请标注下我的link..非常感谢

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值