1. 程式人生 > >C# 樹形遞迴,部門樹狀圖

C# 樹形遞迴,部門樹狀圖

如果資料庫中存在的department部門表,其中ID為主鍵,PID為父類,Name為部門名稱,設計如下:
  1. publicclass department  
  2. {  
  3.     publicint ID { getset; }  
  4.     publicint PID { getset; }  
  5.     publicstring Name { getset; }  
  6. }  
設計遞迴演算法:
  1. privatestring 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字串:
  1. public
     ActionResult Index()  
  2. {  
  3.     return View();  
  4. }  
  5. public ActionResult DeparTmentList()  
  6. {  
  7.     List<department> ds = new List<department>()  
  8.     {  
  9.         new department(){ ID=6, Name="前端開發組1", PID=4},  
  10.         new department(){ ID=1, Name="XX公司", PID=0},  
  11.         new department(){ ID=2, Name=
    "開發部", PID=1},  
  12.         new department(){ ID=3, Name=".NET開發部門", PID=2},  
  13.         new department(){ ID=5, Name=".NET開發組1", PID=3},  
  14.         new department(){ ID=4, Name="前端開發部門", PID=2},  
  15.         new department(){ ID=6, Name=".NET開發組2", PID=3},  
  16.     };  
  17.     return Content(departmentRecursion(0, ds));  
  18. }  
新增Home檢視:
  1. @{  
  2.     Layout = null;  
  3. }  
  4. <!DOCTYPE html>  
  5. <html>  
  6. <head>  
  7.     <meta name="viewport" content="width=device-width" />  
  8.     <title>Index</title>  
  9. </head>  
  10. <body>  
  11.     @Html.Action("DeparTmentList""Home");<span style="white-space:pre">     </span>  
  12. </body>  
  13. </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>