问题是这样的,Department表是一个部门表,由DeptId,name和Father组成,它是一种树型的关系,一个部门下可以有多个子部门,同时,它有一个父部门,祖宗部门没有父部门。
以下是测试数据(相当于Data层里取出数据的方法):
static List<Department> deptList = new List<Department>
{
new Department(1,"根",0),
new Department(2,"计算机",1),
new Department(3,"英语",1),
new Department(4,"C语",2),
new Department(5,"VB",2),
new Department(6,"公共英语",3),
new Department(7,"大学英语",3),
new Department(8,"公一级",6),
new Department(9,"公二级",6),
new Department(10,"公三级",6),
new Department(11,"公四级",6),
};
存放数据源的变量和为变量赋值的方法
#region 加载树,定义存放树的变量
public static void LoadTree()
{
DepartmentTree = deptList;
}
public static List<Department> DepartmentTree = null;
#endregion
我们把加载树放在静态构造方法里,保存它只被加载一次就可以了,当然如果希望树自动更新,可以
看文章结尾处
#region 当前类被第一次访问或类被第一次建立时间时加载树
static Program()
{
LoadTree();
}
#endregion
以下是Service层,进行数据读取的方法:
#region 树递归Service层
/// <summary>
/// 完整树
/// </summary>
/// <returns></returns>
public static Department GetTree()
{
return GetTree(Department.RootId);
}
/// <summary>
/// 找到指定ID的树
/// </summary>
/// <param name="deptId"></param>
/// <returns></returns>
public static Department GetTree(int deptId)
{
Department root = new Department();
root = deptList.Where(i => i.DeptId.Equals(deptId)).SingleOrDefault();
GetSublCategories(root);
return root;
}
/// <summary>
///找到子孙树
/// </summary>
/// <param name="department">父对象</param>
static public void GetSublCategories(Department department)
{
department.Sons = DepartmentTree.Where(item =>
item.FatherId.Equals(department.DeptId) && item.DeptId != Department.RootId).ToList();
department.Sons.ForEach(item =>
{
item.Father = department;
GetSublCategories(item);
});
}
#endregion
以下是UI层,将树型列表显示在屏幕上的代码:
#region 树递归UI层
public string GetTreeHtml()
{
Department department = GetTree();
StringBuilder html = new StringBuilder();
html.Append("<ul id='containerul'>");
this.GetSubCategory(html, department);
html.Append("</ul>");
return html.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="html">HTML字条串组成树型列表</param>
/// <param name="department">指定对象</param>
private void GetSubCategory(StringBuilder html, Department department)
{
html.Append("<li id='" + department.DeptId + "'>");
if (department.Sons != null && department.Sons.Count > 0)
{
html.Append("<a href='javascript:void(0)'>");
}
else
{
html.Append("<a href='javascript:void(0)' onclick=\"showDCategoryTree('"
+ department.DeptId + "');\">");
}
html.Append("<ins> </ins>");
html.Append(department.DeptId + "-" + department.Name);
html.Append("</a>");
if (department.Sons != null && department.Sons.Count > 0)
{
html.Append("<ul>");
foreach (var item in department.Sons)
{
this.GetSubCategory(html, item);
}
html.Append("</ul>");
}
html.Append("</li>");
}
#endregion
如果你的树(部门)前台显示时不会发生读写操作,可以把取数据方法写在静态构造方法里,就像这样:
#region 树递归UI层
public string GetTreeHtml()
{
Department department = GetTree();
StringBuilder html = new StringBuilder();
html.Append("<ul id='containerul'>");
this.GetSubCategory(html, department);
html.Append("</ul>");
return html.ToString();
}
/// <summary>
/// 递归所有子树
/// </summary>
/// <param name="html">HTML字条串组成树型列表</param>
/// <param name="department">指定对象</param>
private void GetSubCategory(StringBuilder html, Department department)
{
html.Append("<li id='" + department.DeptId + "'>");
if (department.Sons != null && department.Sons.Count > 0)
{
html.Append("<a href='javascript:void(0)'>");
}
else
{
html.Append("<a href='javascript:void(0)' onclick=\"showDCategoryTree('"
+ department.DeptId + "');\">");
}
html.Append("<ins> </ins>");
html.Append(department.DeptId + "-" + department.Name);
html.Append("</a>");
if (department.Sons != null && department.Sons.Count > 0)
{
html.Append("<ul>");
foreach (var item in department.Sons)
{
this.GetSubCategory(html, item);
}
html.Append("</ul>");
}
html.Append("</li>");
}
#endregion
OK,这种方法显示出来的树在不关闭浏览器或IIS不重启时,树如果被修改了,它也不会反映到树上的,如果希望改变这种情况,让数的修改实时显示出来,可以加一个事件。
#region 每隔1000毫秒去重新加载树
static System.Timers.Timer sysTimer = new System.Timers.Timer(1000);
static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
LoadTree();
}
#endregion
本文转自博客园张占岭(仓储大叔)的博客,原文链接:数据结构~树的遍历(Service层和UI层代码),如需转载请自行联系原博主。