C# 树形递归,部门树状图

如果数据库中存在的department部门表,其中ID为主键,PID为父类,Name为部门名称,设计如下:
[csharp]  view plain  copy
  1. public class department  
  2. {  
  3.     public int ID { getset; }  
  4.     public int PID { getset; }  
  5.     public string Name { getset; }  
  6. }  
设计递归算法:
[csharp]  view plain  copy
  1. private string departmentRecursion(int pid, List<department> obj)  
  2. {  
  3.     string a = "<dl style='margin:0px;padding:0px;'>";  
  4.     for (int i = 0; i < obj.Count; i++)  
  5.     {  
  6.         if (obj[i].PID == pid)  
  7.         {  
  8.             a += "<dt  style='border-bottom:1px solid #808080'>" + obj[i].Name + "</dt>\n<dd>" + departmentRecursion(obj[i].ID, obj) + "</dd>";  
  9.         }  
  10.     }  
  11.     a += "</dl>";  
  12.     return a;  
  13. }  
在MVC中新建立Home控制器:加入下面代码:DeparTmentList查询出部门表信息到List中,这里准备了测试数据,使用递归返回树状html字符串:
[csharp]  view plain  copy
  1. public ActionResult Index()  
  2. {  
  3.     return View();  
  4. }  
  5.   
  6. public ActionResult DeparTmentList()  
  7. {  
  8.     List<department> ds = new List<department>()  
  9.     {  
  10.         new department(){ ID=6, Name="前端开发组1", PID=4},  
  11.         new department(){ ID=1, Name="XX公司", PID=0},  
  12.         new department(){ ID=2, Name="开发部", PID=1},  
  13.         new department(){ ID=3, Name=".NET开发部门", PID=2},  
  14.         new department(){ ID=5, Name=".NET开发组1", PID=3},  
  15.         new department(){ ID=4, Name="前端开发部门", PID=2},  
  16.         new department(){ ID=6, Name=".NET开发组2", PID=3},  
  17.     };  
  18.     return Content(departmentRecursion(0, ds));  
  19. }  
添加Home视图:
[csharp]  view plain  copy
  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Index</title>  
  11. </head>  
  12. <body>  
  13.     @Html.Action("DeparTmentList""Home");<span style="white-space:pre">     </span>  
  14. </body>  
  15. </html>  
调试运行如下:

使用JS树形递归,部门树状图:

[javascript]  view plain  copy
  1. <script>//数据结构  
  2.     var json = [  
  3.     { id: 1, pid: 0, text: '1(XX公司)' },  
  4.     { id: 2, pid: 4, text: '2.1.1(开发部a-1组)' },  
  5.     { id: 3, pid: 0, text: '2(开发部)' },  
  6.     { id: 4, pid: 3, text: '2.1(开发部a)' },  
  7.     { id: 5, pid: 0, text: '3(人事部)' },  
  8.     { id: 6, pid: 5, text: '3.1(人事部A)' },  
  9.     { id: 7, pid: 0, text: '4(设计部)' },  
  10.     { id: 8, pid: 7, text: '4.1(设计部A)' },  
  11.     { id: 9, pid: 4, text: '2.1.2(开发部a-2组)' },  
  12.     { id: 11, pid: 2, text: '2.1.1.1(开发部a-1组>1小组)' },  
  13.     { id: 12, pid: 2, text: '2.1.1.2(开发部a-1组>2小组)' },  
  14.     { id: 13, pid: 5, text: '3.2(人事部A)' },  
  15.     { id: 19, pid: 5, text: '3.3(人事部B)' }  
  16.     ];  
  17.     function departmentRecursion(mid, obj) {  
  18.         var a = '<dl>';  
  19.         for (var i = 0, j; j = obj[i++];)  
  20.             if (j.pid == mid)  
  21.                 a += '<dt>' + j.text + '</dt><dd>' + departmentRecursion(j.id, obj) + '</dd>'  
  22.         a += '</dl>';  
  23.         return a;  
  24.     }  
  25.     document.write(departmentRecursion(0, json));  
  26. </script>  

猜你喜欢

转载自blog.csdn.net/qmdweb/article/details/80734556