首页 > 代码库 > EasyUI ComboTree数据绑定树形分类显示
EasyUI ComboTree数据绑定树形分类显示
承接上篇博文【LINQ获取树形分类的层数】。在上文中,笔者只分享了层数,在这里我把完整的实现贴出来,欢迎批评指正。
先附上效果图:
首先是Tree公共类
- public class Tree
- {
- public int ModuleID { get; set; }
- public int ParentID { get; set; }
- public int ModulePath { get; set; }
- public string ModuleName { get; set; }
- }
接下来就是TreeHelper了,这个类是获取json数据的核心。他将数据库中的数据组装成了EasyUI可以接受的json格式。
- public class DepartmentTreeHelper
- {
- private static ZhaopinModels db = new ZhaopinModels();
- #region 获取父类集合
- public static IList<Tree> returnParentTree()
- {
- IList<Tree> trees = new List<Tree>();
- List<TUserDepartment> departments = db.TUserDepartments.Where(d => d.department_pid == 0).ToList();
- foreach (var userDepartment in departments)
- {
- Tree tree = new Tree();
- tree.ModuleID = userDepartment.id;
- tree.ParentID = (int) userDepartment.department_pid;
- tree.ModulePath = GetLayerById(tree.ModuleID);
- tree.ModuleName = userDepartment.department_name;
- trees.Add(tree);
- }
- return trees;
- }
- #endregion
- #region 获取分类的层数
- /// <summary>
- /// 获取分类的层数
- /// </summary>
- /// <param name="id">根据id获取树的层数</param>
- /// <returns>树的层数</returns>
- private static int GetLayerById(int id)
- {
- int layer = 1;
- for (
- int subId =
- (int)
- db.TUserDepartments.Where(d => d.id == id)
- .Select(d => d.department_pid)
- .ToList()
- .FirstOrDefault();
- subId != 0;)
- {
- subId =
- (int)
- db.TUserDepartments.Where(d => d.id == subId)
- .Select(d => d.department_pid)
- .ToList()
- .FirstOrDefault();
- layer++;
- }
- return layer;
- }
- #endregion
- #region 判断分类是否有子类
- /// <summary>
- /// 判断分类是否有子类
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public static bool IsHaveChild(int id)
- {
- bool flag = false;
- var did = db.TUserDepartments.Where(d => d.department_pid == id).Select(d => d.id).ToList();
- if (did.Count > 0)
- {
- flag = true;
- }
- return flag;
- }
- #endregion
- #region 根据id获取子类
- /// <summary>
- /// 根据id获取子类
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public static IList<Tree> GetChild(int id)
- {
- IList<Tree> t = new List<Tree>();
- List<TUserDepartment> departments = db.TUserDepartments.Where(d => d.department_pid == id).ToList();
- foreach (var d in departments)
- {
- Tree tParent = new Tree();
- tParent.ModuleID = d.id;
- tParent.ParentID = (int) d.department_pid;
- tParent.ModulePath = DepartmentTreeHelper.GetLayerById(tParent.ModuleID);
- tParent.ModuleName = d.department_name;
- t.Add(tParent);
- }
- return t;
- }
- #endregion
- #region 获取json
- /// <summary>
- /// 获取json
- /// </summary>
- /// <returns></returns>
- public static string GetJson()
- {
- string json = "[";
- IList<Tree> t = DepartmentTreeHelper.returnParentTree();
- foreach (Tree model in t)
- {
- if (model != t[t.Count - 1])
- {
- json += DepartmentTreeHelper.GetJsonByModel(model) + ",";
- }
- else
- {
- json += DepartmentTreeHelper.GetJsonByModel(model);
- }
- }
- json += "]";
- json = json.Replace("‘", "\"");
- return json;
- }
- #endregion
- #region 根据模型生成json
- /// <summary>
- /// 根据模型生成json
- /// </summary>
- /// <param name="t"></param>
- /// <returns></returns>
- public static string GetJsonByModel(Tree t)
- {
- string json = "";
- bool flag = DepartmentTreeHelper.IsHaveChild(t.ModuleID);
- json = "{"
- + "‘id‘:‘" + t.ModuleID + "‘,"
- + "‘text‘:‘" + t.ModuleName + "‘,"
- + "‘iconCls‘:‘ok‘,"
- + "‘children‘:";
- if (!flag)
- {
- json += "null}";
- }
- else
- {
- json += "[";
- IList<Tree> list = DepartmentTreeHelper.GetChild(t.ModuleID);
- foreach (Tree tree in list)
- {
- if (tree != list[list.Count - 1])
- {
- json += GetJsonByModel(tree) + ",";
- }
- else
- {
- json += GetJsonByModel(tree);
- }
- }
- json += "]}";
- }
- return json;
- }
- #endregion
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。