/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{ var sections = new List<Section>
{
new Section { Id = 1, Name = "中国", ParentID = 0 },
new Section { Id = 2, Name = "江西", ParentID = 1 },
new Section { Id = 3, Name = "江苏", ParentID = 1 },
new Section { Id = 4, Name = "南京", ParentID = 3 },
new Section { Id = 5, Name = "南昌", ParentID = 2 },
new Section { Id = 6, Name = "东湖区", ParentID = 5 },
new Section { Id = 7, Name = "广东", ParentID = 1 },
new Section { Id = 8, Name = "深圳", ParentID = 7 },
new Section { Id = 9, Name = "罗湖区涂聚文", ParentID = 8 }
}; //sections = sections.OrderBy(x => x.ParentID).ThenBy(x => x.Name).ToList();
//var stack = new Stack<Section>(); //// Grab all the items without parents
//foreach (var section in sections.Where(x => x.ParentID == default(int)).Reverse())
//{
// stack.Push(section);
// sections.RemoveAt(0);
//} var output = new List<Section>();
//while (stack.Any())
//{
// var currentSection = stack.Pop(); // var children = sections.Where(x => x.ParentID == currentSection.Id).Reverse(); // foreach (var section in children)
// {
// stack.Push(section);
// sections.Remove(section);
// }
// output.Add(currentSection);
//}
//sections = output; List<MySection> mys = MenuHelper.GetMyMenuCollection(sections); //ResolveDDL<MySectionMenu>(mys);
var outputlist = (from mysection in mys orderby mysection.TreeLevel descending select mysection).ToList();
var outputstringlist = (from mysection in mys orderby mysection.TreeLevel descending select mysection.Name).ToList(); for (int i = 0; i < outputlist.Count; i++)
{ //Response.Write(string.Format("ID:{0} ParentID: {1} TreeLevel: {2} Name:{3}<br/>", mys[i].Id, mys[i].ParentID, mys[i].TreeLevel,mys[i].Name));
Response.Write(string.Format("ID:{0} ParentID: {1} TreeLevel: {2} Name:{3}<br/>", outputlist[i].Id, outputlist[i].ParentID, outputlist[i].TreeLevel, outputlist[i].Name));
} } /// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="mys"></param>
protected void ResolveDDL<T>(List<T> mys) where T : MyBaseSection, new()
{ ResolveDDL<T>(mys, -1, true);
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="mys"></param>
/// <param name="currentId"></param>
protected void ResolveDDL<T>(List<T> mys, int currentId) where T : MyBaseSection, new()
{
ResolveDDL<T>(mys, currentId, true);
} /// <summary>
/// 将一个树型结构放在一个下列列表中可供选择
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="currentId"></param>
/// <param name="mys"></param>
protected void ResolveDDL<T>(List<T> mys, int currentId, bool addRootNode) where T : MyBaseSection, new()
{
if (addRootNode)
{
// 所有节点的TreeLevel加一,然后添加根节点
foreach (T my in mys)
{
my.TreeLevel += 1;
}
T root = new T();
root.Name = "--根节点--";
root.Id = 0;
root.TreeLevel = 0;
mys.Insert(0, root);
} // currentId==-1表示当前节点不存在
if (currentId != -1)
{
// 本节点不可点击(也就是说当前节点不可能是当前节点的父节点)
// 并且本节点的所有子节点也不可点击,你想如果当前节点跑到子节点的子节点,那么这些子节点就从树上消失了
bool startChileNode = false;
int startTreeLevel = 0;
foreach (T my in mys)
{
if (my.Id == currentId)
{
startTreeLevel = my.TreeLevel;
my.Enabled = false;
startChileNode = true;
}
else
{
if (startChileNode)
{
if (my.TreeLevel > startTreeLevel)
{
my.Enabled = false;
}
else
{
startChileNode = false;
}
}
}
}
}
} } /// <summary>
/// /
/// </summary>
public class Section
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
}
/// <summary>
///
/// </summary>
public class MySection
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
public int TreeLevel { get; set; }
}
/// <summary>
///
/// </summary>
public class MySectionMenu : MyBaseSection
{ }
/// <summary>
///
/// </summary>
public class MyBaseSection
{
public int Id
{
get;
set;
} public int ParentId
{
get;
set;
} public string Name
{
get;
set;
}
/// <summary>
/// 本菜单在树形结构中层级(从0开始)
/// </summary>
public int TreeLevel
{
get;
set;
} /// <summary>
/// 是否可用(默认true)
/// </summary>
public bool Enabled
{
get;
set;
} /// <summary>
/// 是否叶子节点(默认false)
/// </summary>
public bool IsTreeLeaf
{
get;
set;
} } /// <summary>
///
/// </summary>
public class MenuHelper
{ /// <summary>
///
/// </summary>
/// <param name="oldMenus"></param>
/// <returns></returns>
public static List<MySection> GetMyMenuCollection(List<Section> oldMenus)
{
List<MySection> newMenus = new List<MySection>();
ResolveMenuCollection(oldMenus, newMenus, 0, 0); return newMenus;
}
/// <summary>
///
/// </summary>
/// <param name="oldMenus"></param>
/// <param name="newMenus"></param>
/// <param name="parentId"></param>
/// <param name="level"></param>
/// <returns></returns>
private static int ResolveMenuCollection(List<Section> oldMenus, List<MySection> newMenus, int parentId, int level)
{
int count = 0;
foreach (Section menu in oldMenus)
{
if (menu.ParentID == parentId)
{
count++; MySection my = new MySection();
newMenus.Add(my);
my.TreeLevel = level;
my.Id = menu.Id;
my.Name = menu.Name;
my.ParentID = menu.ParentID; level++;
int childCount = ResolveMenuCollection(oldMenus, newMenus, menu.Id, level);
level--;
}
} return count;
}
}

  

显示结果:

ID:1 ParentID: 0 TreeLevel: 0 Name:中国
ID:2 ParentID: 1 TreeLevel: 1 Name:江西
ID:5 ParentID: 2 TreeLevel: 2 Name:南昌
ID:6 ParentID: 5 TreeLevel: 3 Name:东湖区
ID:3 ParentID: 1 TreeLevel: 1 Name:江苏
ID:4 ParentID: 3 TreeLevel: 2 Name:南京
ID:7 ParentID: 1 TreeLevel: 1 Name:广东
ID:8 ParentID: 7 TreeLevel: 2 Name:深圳
ID:9 ParentID: 8 TreeLevel: 3 Name:涂聚文

csharp: DataRelation objects to represent a parent/child/Level relationship的更多相关文章

  1. [NHibernate]Parent/Child

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...

  2. 服务启动Apache服务,错误Parent: child process exited with status 3 -- Aborting.解决

    不能启动apache,或者使用wamp等集成包后,唯独apache服务启动后有停止,但是把东西搬到其他机器上却没事问题可能和网络有关,我查了很多资料首先找打apache的错误报告日志,发现现实诸多的调 ...

  3. Effective Java 21 Use function objects to represent strategies

    Theory In the Java the function pointers is implemented by the declaring an interface to represent s ...

  4. XPath学习:parent,child

    XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...

  5. NHibernate讲解

    第1章 NHibernate体系结构 总览 对NHibernate体系结构的非常高层的概览: 这幅图展示了NHibernate使用数据库和配置文件数据来为应用程序提供持久化服务(和持久化的对象). 我 ...

  6. Introspection in Python How to spy on your Python objects Guide to Python introspection

    Guide to Python introspection https://www.ibm.com/developerworks/library/l-pyint/ Guide to Python in ...

  7. TIJ——Chapter One:Introduction to Objects

    ///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...

  8. Exploring Python Code Objects

    Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Ins ...

  9. Accessing an element's parent with ElementTree(转)

    Today I ran across a situation where I needed to programmatically remove specific elements from a KM ...

随机推荐

  1. openseadragon.js与deep zoom java实现艺术品图片展示

    openseadragon.js 是一款用来做图像缩放的插件,它可以用来做图片展示,做展示的插件很多,也很优秀,但大多数都解决不了图片尺寸过大的问题. 艺术品图像展示就是最简单的例子,展示此类图片一般 ...

  2. [javaweb]Java过滤器与包装设计模式的实用案例.

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  3. iOS---NSAutoreleasePool自动释放原理及详解

    前言:当您向一个对象发送一个autorelease消息时,Cocoa就会将该对象的一个引用放入到最新的自动释放池.它仍然是个正当的对象,因此自动释放池 定义的作用域内的其它对象可以向它发送消息.当程序 ...

  4. 每天一个linux命令(17):whereis 命令

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...

  5. KnockoutJS 3.X API 第二章 数据监控(1)视图模型与监控

    数据监控 KO的三个内置核心功能: 监控(Observable)和依赖性跟踪(dependency tracking) 声明绑定(Declarative bindings) 模板(Templating ...

  6. 理解与模拟一个简单web服务器

    先简单说下几个概念,根据自己的理解,不正确请见谅. web服务器 首先要知道什么是web服务器,简单说web服务器就是可以使用HTTP传输协议与客户端进行通信的服务器.最初的web服务器只能用来处理静 ...

  7. 详解JavaScript模块化开发

    什么是模块化开发? 前端开发中,起初只要在script标签中嵌入几十上百行代码就能实现一些基本的交互效果,后来js得到重视,应用也广泛起来了,jQuery,Ajax,Node.Js,MVC,MVVM等 ...

  8. JavaScript与有限状态机

    有限状态机(Finite-state machine)是一个非常有用的模型,可以模拟世界上大部分事物. 简单说,它有三个特征: * 状态总数(state)是有限的. * 任一时刻,只处在一种状态之中. ...

  9. Android入门(十四)内容提供器-实现跨程序共享实例

    原文链接:http://www.orlion.ga/661/ 打开SQLite博文中创建的 DatabaseDemo项目,首先将 MyDatabaseHelper中使用 Toast弹出创建数据库成功的 ...

  10. 什么才是正确的javascript数组检测方式

    前面的话 对于确定某个对象是不是数组,一直是数组的一个经典问题.本文专门将该问题择出来,介绍什么才是正确的javascript数组检测方式 typeof 首先,使用最常用的类型检测工具——typeof ...