溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于JQuery的Dynatree解決MVC中對(duì)樹(shù)形結(jié)構(gòu)的解決方案

發(fā)布時(shí)間:2020-07-15 19:37:53 來(lái)源:網(wǎng)絡(luò) 閱讀:2379 作者:zachary_woo 欄目:web開(kāi)發(fā)

由于日常WEB開(kāi)發(fā)中常會(huì)用到樹(shù)形來(lái)完成以下主要功能。

 

主要解決以下一些功能

  1. 數(shù)據(jù)結(jié)構(gòu)的樹(shù)形層級(jí)展示
  2. 多選項(xiàng)目
  3. 單選項(xiàng)目
  4. 方便Ajax延遲加載

 

經(jīng)過(guò)幾個(gè)插件的比較最后決定使用dynatree。來(lái)完成以上功能。

dynatree項(xiàng)目網(wǎng)站 https://code.google.com/p/dynatree/

本文中dynatree的版本為1.2.4

首先通常代碼中會(huì)有一個(gè)樹(shù)形結(jié)構(gòu)的實(shí)體對(duì)象如下代碼: 

  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方式所以我打算在后臺(tái)的controller中返回json的方式來(lái)完成對(duì)tree的數(shù)據(jù)加載
于是為了方便dynatree的前臺(tái)接受,我做了一個(gè)封裝類代碼如下

  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 p_w_picpath (filename relative to tree.options.p_w_picpathPath). '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.     } 

因?yàn)閖avascript對(duì)大小寫敏感所以這里我將屬性都改成小寫已達(dá)到和dynatree的children參數(shù)統(tǒng)一。

接下來(lái)控制器很簡(jiǎn)單的返回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. <h3>Index</h3> 
  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> 

一顆簡(jiǎn)單的多選樹(shù)就這么完成了,如果要單選只需將參數(shù)selectMode設(shè)置為1

 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI