基于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. { 
  3.     public int ID { get; set; } 
  4.  
  5.     public string Name { get; set; } 
  6.  
  7.     public string Description { get; set; } 
  8.  
  9.     public List Children { get; set; } 
  10.  
  11.     public Node Parent { get; set; } 
  12. } 

由于我们采用AJAX方式所以我打算在后台的controller中返回json的方式来完成对tree的数据加载
于是为了方便dynatree的前台接受,我做了一个封装类代码如下

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

因为javascript对大小写敏感所以这里我将属性都改成小写已达到和dynatree的children参数统一。

接下来控制器很简单的返回json即可,代码如下:

 

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

在view视图中我们只要加载jquery,jqueryUI和dynatree.js
由于dynatree的checkbox等使用样式写的,所以也必须引用dynatree.css
视图view的代码如下:

  1. @{ 
  2.     ViewBag.Title = "Index"; 
  3.     Layout = "~/Views/Shared/_Layout.cshtml"; 
  4. } 
  5.  
  6. Index

     
   
  • @section scripts{ 
  •      
  •      
  •      
  •         $(function () { 
  •             $("#tree").dynatree({ 
  •                 checkbox: true, 
  •                 selectMode: 2, 
  •                 initAjax: { url: '/home/ajaxTree' }, 
  •                 onSelect: function (select, node) { 
  •                     if (select) { 
  •                         alert(node.data.title)                         
  •                     } 
  •                 } 
  •             }); 
  •         }); 
  •      
  • } 
  • 一颗简单的多选树就这么完成了,如果要单选只需将参数selectMode设置为1

     


    分享文章:基于JQuery的Dynatree解决MVC中对树形结构的解决方案
    文章来源:http://jxruijie.cn/article/gggics.html

    其他资讯