基於JQuery的Dynatree解決MVC中對樹形結構的解決方案

由於日常WEB開發中常會用到樹形來完成以下主要功能。

 

主要解決以下一些功能

  1. 數據結構的樹形層級展示
  2. 多選項目
  3. 單選項目
  4. 方便Ajax延遲加載

 

經過幾個插件的比較最後決定使用dynatree。來完成以上功能。

dynatree項目網站 https://code.google.com/p/dynatree/

本文中dynatree的版本爲1.2.4

首先通常代碼中會有一個樹形結構的實體對象如下代碼: 

  1. public class Node 
  2.     public int ID { getset; } 
  3.  
  4.     public string Name { getset; } 
  5.  
  6.     public string Description { getset; } 
  7.  
  8.     public List<Node> Children { getset; } 
  9.  
  10.     public Node Parent { getset; } 

由於我們採用AJAX方式所以我打算在後臺的controller中返回json的方式來完成對tree的數據加載
於是爲了方便dynatree的前臺接受,我做了一個封裝類代碼如下

  1. public class DynatreeNode 
  2.     private DynatreeNode() 
  3.     { 
  4.         children = new List<DynatreeNode>(); 
  5.     } 
  6.  
  7.     #region property 
  8.     /// <summary> 
  9.     /// (required) Displayed name of the node (html is allowed here) 
  10.     /// </summary> 
  11.     public string title { getset; } 
  12.  
  13.     /// <summary> 
  14.     /// tooltip: null, // Show this popup text. 
  15.     /// </summary> 
  16.     public string tooltip { getset; } 
  17.  
  18.     /// <summary> 
  19.     /// href: null, // Added to the generated a tag. 
  20.     /// </summary> 
  21.     public string href { getset; } 
  22.  
  23.     /// <summary> 
  24.     /// icon: null, // Use a custom image (filename relative to tree.options.imagePath). 'null' for default icon, 'false' for no icon. 
  25.     /// </summary> 
  26.     public string icon { getset; } 
  27.  
  28.     /// <summary> 
  29.     /// addClass: null, // Class name added to the node's span tag.     
  30.     /// </summary> 
  31.     public string addClass { getset; } 
  32.  
  33.     /// <summary> 
  34.     ///  children: null // Array of child nodes. 
  35.     /// </summary> 
  36.     public List<DynatreeNode> children { getset; } 
  37.  
  38.     /// <summary> 
  39.     /// unselectable: false, // Prevent selection. 
  40.     /// </summary> 
  41.     public bool unselectable { getset; } 
  42.  
  43.     /// <summary> 
  44.     /// hideCheckbox: false, // Suppress checkbox display for this node. 
  45.     /// </summary> 
  46.     public bool hideCheckbox { getset; } 
  47.  
  48.     /// <summary> 
  49.     /// select: false, // Initial selected status. 
  50.     /// </summary> 
  51.     public bool select { getset; } 
  52.  
  53.     /// <summary> 
  54.     /// May be used with activate(), select(), find(), ... 
  55.     /// </summary> 
  56.     public string key { getset; } 
  57.  
  58.     /// <summary> 
  59.     /// expand: false, // Initial expanded status. 
  60.     /// </summary> 
  61.     public bool expand { getset; } 
  62.  
  63.     /// <summary> 
  64.     /// focus: false, // Initial focused status. 
  65.     /// </summary> 
  66.     public bool focus { getset; } 
  67.  
  68.     /// <summary> 
  69.     /// Use a folder icon. Also the node is expandable but not selectable.false 
  70.     /// </summary> 
  71.     public bool isFolder { getset; } 
  72.  
  73.     /// <summary> 
  74.     /// isLazy: false,  Call onLazyRead(), when the node is expanded for the first time to allow for delayed 
  75.     /// </summary> 
  76.     public bool isLazy { getset; } 
  77.  
  78.     /// <summary> 
  79.     /// noLink: false, // Use span instead of a tag for this node 
  80.     /// </summary> 
  81.     public bool noLink { getset; } 
  82.  
  83.     /// <summary> 
  84.     /// activate: false, // Initial active status. 
  85.     /// </summary> 
  86.     public bool activate { getset; } 
  87.     #endregion 
  88.  
  89.     public static DynatreeNode Create(Node node) 
  90.     { 
  91.         DynatreeNode dynatreeNode = new DynatreeNode 
  92.         { 
  93.             title = node.Name, 
  94.             tooltip = node.Name, 
  95.             activate = false
  96.             addClass = null
  97.             expand = false
  98.             focus = false
  99.             icon = null
  100.             href = null
  101.             key = null
  102.             unselectable = false
  103.             select = false
  104.             noLink = false
  105.             isLazy = false
  106.             hideCheckbox = false
  107.             isFolder = false 
  108.         }; 
  109.         foreach (var item in node.Children) 
  110.         { 
  111.             dynatreeNode.isFolder = true
  112.             dynatreeNode.children.Add(DynatreeNode.Create(item)); 
  113.         } 
  114.         return dynatreeNode; 
  115.     } 

因爲javascript對大小寫敏感所以這裏我將屬性都改成小寫已達到和dynatree的children參數統一。

接下來控制器很簡單的返回json即可,代碼如下:

 

  1. public ActionResult AjaxTree() 
  2.         root = GetTreeRoot(); 
  3.         var dynatreeNode = DynatreeNode.Create(root); 
  4.         return Json(dynatreeNode, JsonRequestBehavior.AllowGet); 

在view視圖中我們只要加載jquery,jqueryUI和dynatree.js
由於dynatree的checkbox等使用樣式寫的,所以也必須引用dynatree.css
視圖view的代碼如下:

  1. @{ 
  2.     ViewBag.Title = "Index"
  3.     Layout = "~/Views/Shared/_Layout.cshtml"
  4.  
  5. <h2>Index</h2> 
  6. <div id="tree"></div>   
  7. @section scripts{ 
  8.     <link href="~/Content/skin-vista/ui.dynatree.css" rel="stylesheet" /> 
  9.     <script src="~/Scripts/jquery.dynatree.js"></script> 
  10.     <script type="text/ecmascript"> 
  11.         $(function () { 
  12.             $("#tree").dynatree({ 
  13.                 checkbox: true, 
  14.                 selectMode: 2, 
  15.                 initAjax: { url: '/home/ajaxTree' }, 
  16.                 onSelect: function (select, node) { 
  17.                     if (select) { 
  18.                         alert(node.data.title)                         
  19.                     } 
  20.                 } 
  21.             }); 
  22.         }); 
  23.     </script> 

一顆簡單的多選樹就這麼完成了,如果要單選只需將參數selectMode設置爲1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章