拼吾爱程序人生

首页»  jQuery»  jQuery TreeView
cobra - 2009-3-10 13:48:00
找了一个jQuery的树形插件,自己添加了增、删、改、查,与数据库交互(SQL Server 2005)。

支持3层节点,要扩展也可以,并不完善,欢迎提出意见

截图:

附件: 2009031013402629.jpg



客户端主要代码:
  1. <div id="divMove">
  2.     <input id="btnAdd" type="button" value="添加" /><input id="btnDel" type="button" value="删除" /></div>
  3. <div id="divTree">
  4.     <ul id="red" class="treeview-red">
  5.     </ul>
  6. </div>
复制代码
以后变更的树形内容都在ul下面。

主要的js文件代码:
  1. //--Author:Vincent

  2. $(document).ready(function(){   
  3. //bulid xmlHttpRequest
  4.     var xmlHttp;
  5.     var oClick;//which mouse up
  6.     if(window.ActiveXObject)
  7.     {
  8.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  9.     }
  10.     else if(window.XMLHttpRequest)
  11.     {
  12.         xmlHttp=new XMLHttpRequest();
  13.     }
  14.     var post="op=2";//bind data from database
  15.     var url=document.location.href;
  16.     xmlHttp.open("POST",url,true);
  17.     xmlHttp.onreadystatechange=callback;
  18.     xmlHttp.setRequestHeader("Content-Length",post.length);
  19.     xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  20.     xmlHttp.send(post); 
  21.        
  22.     //add
  23.     $("#btnAdd").click(function(event){
  24. //        var oUL=document.getElementById("red");
  25. //        var oLI=document.createElement("li");
  26. //        var oInput=document.createElement("input");
  27. //       
  28. //        oInput.setAttribute("class","treenode");
  29. //        oInput.setAttribute("value","New Node");
  30. //        oInput.setAttribute("type","text");
  31. //       
  32. //        oLI.appendChild(oInput);
  33. //        oUL.appendChild(oLI);
  34.         var oNode=oClick;
  35.         var array=new Array();
  36.              
  37.         for(var i=0;i<document.childNodes.length;i++)
  38.         {
  39.             if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
  40.             {
  41.                 var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
  42.                 array=oo;
  43.                 oNode=oNode.parentNode.parentNode;
  44.             }
  45.             else
  46.                 break;
  47.         }
  48.         if(array.length==0)
  49.         {
  50.             post="op=1&sort="+oClick.value+"|NewNode1.0|NewNode1.0.0";
  51.         }
  52.         else if(array.length==1)
  53.         {
  54.             post="op=1&sort="+array[0]+"|"+oClick.value+"|NewNode1.0.0";
  55.         }
  56.         else if(array.length==2)
  57.         {
  58.             post="op=1&sort="+array[1]+"|"+array[0]+"|NewNode1.0.0";
  59.         }
  60.        
  61.         //post="op=1&sort=NewNode1.0|NewNode1|NewNode1.0.0";//add new data
  62.         xmlHttp.open("POST",url,true);
  63.         xmlHttp.onreadystatechange=callback;
  64.         xmlHttp.setRequestHeader("Content-Length",post.length);
  65.         xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  66.         xmlHttp.send(post);
  67.     });
  68.     //delete
  69.     $("#btnDel").click(function(event){
  70.        
  71.         if(confirm('Are you sure to clear or delete?'))
  72.         { 
  73.             var oNode=oClick;
  74.             var array=new Array();
  75.              
  76.             for(var i=0;i<document.childNodes.length;i++)
  77.             {
  78.                 if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
  79.                 {
  80.                     var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
  81.                     array=oo;
  82.                     oNode=oNode.parentNode.parentNode;
  83.                 }
  84.                 else
  85.                     break;
  86.             }
  87.             post="op=4&sort=";
  88.            
  89.             for(var i=0;i<array.length;i++)
  90.             {
  91.                 post+=array+"|";
  92.                
  93.                 if(i==array.length-1)
  94.                 {
  95.                     post=post.substring(0,post.length-1)+"|"+oClick.value;
  96.                 }
  97.             }
  98.             if(array.length==0)
  99.             {
  100.                 alert('Can not to be deleted!');
  101.                 return;
  102.             }
  103.             xmlHttp.open("POST",url,true);
  104.             xmlHttp.onreadystatechange=callback;
  105.             xmlHttp.setRequestHeader("Content-Length",post.length);
  106.             xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  107.             xmlHttp.send(post);
  108.         }
  109.     });
  110.     //web visit callback
  111.     function callback()
  112.     {
  113.         if(xmlHttp.readystate==4)
  114.         {
  115.             if(xmlHttp.status==200)
  116.             {
  117.                 display();
  118.             }
  119.             else
  120.             {
  121.                 alert("Server Back Status:"+xmlHttp.statusText);
  122.             }
  123.         }
  124.         else//status is not success
  125.         {
  126.             document.getElementById("red").innerHTML="Data Loading";
  127.         }
  128.     }
  129.     function display()
  130.     {
  131.         var index=xmlHttp.responseText.indexOf("<!DOCTYPE");
  132.         var html=xmlHttp.responseText.substring(0,index);
  133.         document.getElementById("red").innerHTML=html;
  134.        
  135.         reset();
  136.     }
  137.        
  138.     function reset()
  139.     {   
  140.         $("#red").treeview({
  141.             animated: "fast",
  142.             collapsed: true,
  143.             unique: true,
  144.             persist: "cookie",
  145.             toggle: function() {
  146.                 window.console && console.log("%o was toggled", this);
  147.             }
  148.         });
  149.    
  150.         //double click textbox
  151.         $("#divTree input").dblclick(function(event){
  152.             $(this).addClass("focus");
  153.            
  154.             var oDiv=document.getElementById("divMove");
  155.             oDiv.style.display="none";
  156.         });
  157.         //textbox mouseout
  158.         $("#divTree input").mouseout(function(event){
  159.             $(this).removeClass("focus");
  160.         });
  161.        
  162.         //textbox enter
  163.         $("#divTree input").keydown(function(event){
  164.             if(event.keyCode==13)
  165.             {
  166.                 $(this).removeClass("focus");
  167.            
  168.                 var oNode=this;
  169.                 var array=new Array();
  170.                  
  171.                 for(var i=0;i<document.childNodes.length;i++)
  172.                 {
  173.                     if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
  174.                     {
  175.                         var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
  176.                         array=oo;
  177.                         oNode=oNode.parentNode.parentNode;
  178.                     }
  179.                     else
  180.                         break;
  181.                 }
  182.                 post="op=3&sort=";
  183.            
  184.                 for(var i=0;i<array.length;i++)
  185.                 {
  186.                     post+=array+"|";
  187.                
  188.                     if(i==array.length-1)
  189.                     {
  190.                         post=post.substring(0,post.length-1)+"|"+this.value+"|"+this.defaultValue;
  191.                     }
  192.                 }
  193.                 if(array.length==0) post+=this.value+"|"+this.defaultValue;
  194.                 xmlHttp.open("POST",url,true);
  195.                 xmlHttp.onreadystatechange=callback;
  196.                 xmlHttp.setRequestHeader("Content-Length",post.length);
  197.                 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  198.                 xmlHttp.send(post);
  199.             }
  200.         });
  201.    
  202.         $(".treenode").mouseup(function(event){
  203.        
  204.             var oDiv=document.getElementById("divMove");
  205.        
  206.             oDiv.style.left=event.clientX;
  207.             oDiv.style.top=event.clientY;
  208.             oDiv.style.display="block";
  209.        
  210.             oClick=$(this).get(0);
  211.             //alert(event.clientY+" "+event.clientX);
  212.        
  213.         });
  214.         $("#divTree").mouseover(function(event){
  215.             var oDiv=document.getElementById("divMove");
  216.             oDiv.style.display="none";
  217.         });
  218.     }
  219. });
复制代码
实现了异步通知后台对数据库的增、删、改、查

后台代码:
  1. public partial class Test: System.Web.UI.Page
  2.     {
  3.         protected void Page_Load(object sender, EventArgs e)
  4.         {
  5.             if (!IsPostBack)
  6.             {
  7.                 try
  8.                 {
  9.                     int op = Int32.Parse(Request.Form["op"]);

  10.                     switch (op)
  11.                     {
  12.                         case 1:
  13.                             AddSort();
  14.                             break;
  15.                         case 2:
  16.                             GetSort();
  17.                             break;
  18.                         case 3:
  19.                             UpSort();
  20.                             break;
  21.                         case 4:
  22.                             DelSort();
  23.                             break;
  24.                     }
  25.                 }
  26.                 catch (Exception excpt){ }
  27.             }
  28.         }

  29.         /// <summary>
  30.         /// 删除分类
  31.         /// </summary>
  32.         private void DelSort()
  33.         {
  34.             string line = Request.Form["sort"].ToString();
  35.             string[] sorts = line.Split('|').ToArray();

  36.             switch (sorts.Length)
  37.             {
  38.                 case 2:
  39.                     DelType(sorts);
  40.                     break;
  41.                 case 3:
  42.                     DelContent(sorts);
  43.                     break;
  44.             }
  45.             GetSort();       
  46.         }

  47.         /// <summary>
  48.         /// 删除具体内容
  49.         /// </summary>
  50.         /// <param name="sorts"></param>
  51.         private void DelContent(string[] sorts)
  52.         {
  53.             string type = sorts[0];
  54.             string sort = sorts[1];
  55.             string content = sorts[2];

  56.             MerchandiseSort ms = new MerchandiseSort();
  57.             MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);

  58.             try
  59.             {
  60.                 ms.Delete(msInfo);
  61.             }
  62.             catch { }
  63.         }

  64.         /// <summary>
  65.         /// 删除分类类型
  66.         /// </summary>
  67.         /// <param name="sorts"></param>
  68.         private void DelType(string[] sorts)
  69.         {
  70.             string sort = sorts[0];
  71.             string type = sorts[1];
  72.             MerchandiseSort ms = new MerchandiseSort();

  73.             try
  74.             {
  75.                 ms.Delete(sort, type);
  76.             }
  77.             catch { }
  78.         }

  79.         /// <summary>
  80.         /// 更新分类
  81.         /// </summary>
  82.         private void UpSort()
  83.         {
  84.             string line = Request.Form["sort"].ToString();
  85.             string[] sorts = line.Split('|').ToArray();

  86.             switch (sorts.Length)
  87.             {
  88.                 case 0:
  89.                     return;
  90.                     break;
  91.                 case 2:
  92.                     UpCCJOY(sorts);
  93.                     break;
  94.                 case 3:
  95.                     UpType(sorts);
  96.                     break;
  97.                 case 4:
  98.                     UpContent(sorts);
  99.                     break;
  100.             }
  101.             GetSort();       
  102.         }

  103.         /// <summary>
  104.         /// 更新分类内容
  105.         /// </summary>
  106.         /// <param name="sorts"></param>
  107.         private void UpContent(string[] sorts)
  108.         {
  109.             string type = sorts[0];
  110.             string sort = sorts[1];
  111.             string content = sorts[2];
  112.             string defaultValue = sorts[3];

  113.             MerchandiseSort ms = new MerchandiseSort();
  114.             MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);

  115.             try
  116.             {
  117.                 ms.Update(msInfo, defaultValue);
  118.             }
  119.             catch { }
  120.         }

  121.         /// <summary>
  122.         /// 更新分类类型
  123.         /// </summary>
  124.         /// <param name="sorts"></param>
  125.         private void UpType(string[] sorts)
  126.         {
  127.             string sort = sorts[0];
  128.             string type = sorts[1];
  129.             string defaultValue = sorts[2];
  130.             MerchandiseSort ms = new MerchandiseSort();

  131.             try
  132.             {
  133.                 ms.Update(sort, type, defaultValue);
  134.             }
  135.             catch { }
  136.         }

  137.         /// <summary>
  138.         /// 更新ccjoy分类
  139.         /// </summary>
  140.         /// <param name="sorts"></param>
  141.         private void UpCCJOY(string[] sorts)
  142.         {
  143.             string sort = sorts[0];
  144.             string defaultValue = sorts[1];
  145.             MerchandiseSort ms = new MerchandiseSort();

  146.             try
  147.             {
  148.                 ms.Update(sort, defaultValue);
  149.             }
  150.             catch { }
  151.         }

  152.         /// <summary>
  153.         /// 获取所有分类
  154.         /// </summary>
  155.         private void GetSort()
  156.         {
  157.             MerchandiseSort ms = new MerchandiseSort();
  158.             IList<MerchandiseSortInfo> msInfoes = new List<MerchandiseSortInfo>();

  159.             try
  160.             {
  161.                 msInfoes = ms.GetMerchandiseSort();
  162.             }
  163.             catch (Exception excpt){ }

  164.             string rep = GetXml(msInfoes);
  165.             Response.Write(rep);
  166.         }

  167.         /// <summary>
  168.         /// 拼取response
  169.         /// </summary>
  170.         /// <param name="msInfoes"></param>
  171.         /// <returns></returns>
  172.         private string GetXml(IList<MerchandiseSortInfo> msInfoes)
  173.         {
  174.             StringBuilder sb = new StringBuilder();

  175.             foreach (MerchandiseSortInfo msInfo in msInfoes)
  176.             {
  177.                 int indexSort = sb.ToString().IndexOf(msInfo.CcjoySort);

  178.                 if (indexSort > -1)
  179.                 {
  180.                     int indexType = sb.ToString().IndexOf(msInfo.Type, indexSort);
  181.                     int indexUL;
  182.                     int indexNextUL;
  183.                     string tmp = string.Empty;
  184.                     string tmpNew = string.Empty;

  185.                     if (indexType > -1)
  186.                     {
  187.                         indexUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexSort);
  188.                         //indexUL = sb.ToString().IndexOf("</ul>", indexUL + 5);

  189.                         if (indexType > indexUL) indexType = -1;
  190.                     }

  191.                     if (indexType > -1)
  192.                     {
  193.                         indexUL = sb.ToString().IndexOf("<ul>", indexType);
  194.                         indexNextUL = sb.ToString().IndexOf("</ul>", indexUL);
  195.                         tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort);
  196.                         tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /></li>", msInfo.Content);
  197.                         sb = sb.Replace(tmp, tmpNew);
  198.                     }
  199.                     else
  200.                     {
  201.                         indexUL = sb.ToString().IndexOf("<ul>", indexSort);
  202.                         indexNextUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexUL);
  203.                         //indexNextUL = sb.ToString().IndexOf("</ul>", indexNextUL + 5);
  204.                         tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort + 15);
  205.                         tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /></li></ul></li>", msInfo.Type, msInfo.Content);
  206.                         sb = sb.Replace(tmp, tmpNew);
  207.                     }
  208.                 }
  209.                 else
  210.                 {
  211.                     sb.Append(string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /><ul><li><input class='treenode' value='{2}' type='text' /></li></ul></li></ul></li>", msInfo.CcjoySort, msInfo.Type, msInfo.Content));
  212.                 }
  213.             }
  214.             return sb.ToString();
  215.         }

  216.         /// <summary>
  217.         /// 添加分类
  218.         /// </summary>
  219.         private void AddSort()
  220.         {
  221.             string line = Request.Form["sort"].ToString();
  222.             string[] sorts = line.Split('|').ToArray();

  223.             if(sorts.Length<3) return;
  224.             string content=sorts[2];
  225.             string type=sorts[1];
  226.             string sort=sorts[0];

  227.             MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
  228.             MerchandiseSort ms = new MerchandiseSort();

  229.             try
  230.             {
  231.                 ms.Add(msInfo);
  232.             }
  233.             catch { }

  234.             GetSort();
  235.         }
  236.     }
复制代码
(文/W2N

用到的js文件、css文件、图片文件:

附件: 下载地址
jQuery栏目的最新浏览文章:
• 《锋利的jQuery》完整版PDF及源码下载
• jQuery in Action(jQuery实战)PDF及源码下载
• jQuery+Ajax实现批量上传图片
• 使用jQuery, CSS, JSON 和Asp.Net打造雅虎图片新闻轮换控件
• jQuery编写的JavaScript图像画廊插件Galleria
• 基于jQuery的窗口插件:jMessageBox
• 齐全的jQuery表单验证插件
• jQuery基础教程(Learning jQuery)中文完整PDF版及源码下载
• jQuery无缝图片横向滚动代码
• DragResize - Resize 和 Drag 类的合并优化版
• 可以替代ExtJS的jQuery UI开源库:jQuery EasyUI 1.0.5
• jQuery 1.4.2 发布:继续提升性能
• [JQuery插件] 固定表头, 左侧某些列的表格
• jQuery 1.4.1 简体中文提示(含下载)
• 用一个jQuery插件EasyDrag轻松实现JS拖动的效果
• 浮动留言板(JQuery 插件)
查看完整版本: jQuery TreeView