zoukankan      html  css  js  c++  java
  • 数据结构~在页面上渲染树型结构

    标准树的代码
      1: class Tree  
    
      2:         {       
    
      3:             static Data.test.ICategoriesRepository iCategoriesRepository = new Data.test.CategoriesRepository(); 
    
      4:             /// <summary>
    
      5:             /// 树形数据对象列表   
    
      6:             /// </summary>
    
      7:              static List<Entity.test.Category> list = null;       
    
      8:            /// <summary>    
    
      9:            /// 获取当前树形数据对象列表   
    
     10:            /// </summary>   
    
     11:             internal static List<Entity.test.Category> ProductVirtualCategoryList 
    
     12:             {
    
     13:                 get { return list; }
    
     14:             }     
    
     15:             static Tree() 
    
     16:             {
    
     17:                 Reload();
    
     18:             }
    
     19:             /// <summary> 
    
     20:             /// 重新加载分类树型列表     
    
     21:             /// </summary>
    
     22:            internal static void Reload()  
    
     23:            {
    
     24:                list = iCategoriesRepository.GetCategories().ToList();   
    
     25:            }    
    
     26:             /// <summary>    
    
     27:             ///  根据父对象,找到子孙树,通过递归所到所有子对象,并把对象赋给参数father   
    
     28:             ///  </summary>   
    
     29:             ///  <param name="father">父对象</param>    
    
     30:             static internal void GetSublCategories(Entity.test.Category father)   
    
     31:             {      
    
     32:                 father.SubCategoryies = list.Where(item => item.ParentId.Equals(father.Id)).ToList();   
    
     33:                 father.SubCategoryies.ForEach(item =>  {item.FatherCategory = father;  
    
     34:                     GetSublCategories(item);   
    
     35:                 });    
    
     36:             }   
    
     37:         }
     树的组装,与前台HTML的拼接
      1:    #region 树的遍历(递归思想)  
    
      2:     Entity.test.Category list = new Entity.test.Category { Id = 1 };   
    
      3:     Tree.GetSublCategories(list);//为list填充子树        
    
      4:     StringBuilder html = new StringBuilder();  
    
      5:     html.Append("<ul>");  
    
      6:     GetSubCategory(html, list); 
    
      7:     html.Append("</ul>");    
    
      8:     #endregion    
    
      9:     Console.WriteLine(html);
    
    树的显示时用到的递归方法
    
     
    
      1: /// <summary>
    
      2: /// 通过递归拼树形结构
    
      3: /// </summary>
    
      4: /// <param name="html"></param>
    
      5: /// <param name="category"></param>
    
      6: private static void GetSubCategory(StringBuilder html, Entity.test.Category category)   
    
      7:    {       
    
      8:     html.Append("<li>");    
    
      9:     html.Append("<input type='hidden' value='" + category.Id + "' />");     
    
     10:     html.Append("<a href='#' name='" + category.Name + "'>");    
    
     11:     html.Append("<ins>&nbsp;</ins>");       
    
     12:     html.Append(category.Id + "-" + category.Name + "(" + category.Level + ")");    
    
     13:     html.Append("</a>");      
    
     14:     if (category.SubCategoryies != null && category.SubCategoryies.Count > 0)        
    
     15:      {         
    
     16:     html.Append("<ul>");    
    
     17:     foreach (var item in category.SubCategoryies)     
    
     18:      {       
    
     19:        GetSubCategory(html, item);     
    
     20:      }            
    
     21:     html.Append("</ul>"); 
    
     22:    }        
    
     23:     html.Append("</li>"); 
    
     24:   }

    返回目录

  • 相关阅读:
    nginx error_log报错upstream timed out (110: Connection timed out)
    ubuntu12.04 nginx添加ssl认证
    TP5.0:同一个控制器访问不同方法
    TP5.0:的安装与配置
    html常用的小技能
    详情介绍win7:编辑文件夹时提示操作无法完成,因为其中的文件夹或文件已在另一个程序中打开的解决过程
    详细讲解:通过phpstudy 设置站点域名、站点域名管理
    tp3.2.3自定义全局函数的使用
    详细讲解:tp3.2.3生成验证码并进行验证(ajax校验返回及自定义返回)
    WPS去掉英语单词下面的红斜线
  • 原文地址:https://www.cnblogs.com/lori/p/2118076.html
Copyright © 2011-2022 走看看