• HTML树


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace zyl
    {
        namespace Html
        {
            public class Tree
            {
                private List<TreeNode> list = new List<TreeNode>();
                public Tree(List<TreeNode> list)
                {
                    this.list = list;
                }
    
                private string AddTree(string id, List<TreeNode> trees, Func<TreeNode, string> noFunc, Func<TreeNode, string> func)//,Func<TreeNode,string> func)
                {
                    StringBuilder treeHtml = new StringBuilder();
                    var childListById = trees.Where(p => p.parentId == id);
                    if (childListById.ToList().Count <= 0)
                    {
                        childListById = trees.Where(p => p.id == id);
                        return "<li>"+noFunc(childListById.ToList()[0])+"</li>";
                    }
                    foreach (var item in childListById)
                    {
                        if (trees.Select(m => m.parentId).Count(m => m == item.id) > 0)
                        {
                            item.isChild = true;
                            treeHtml.Append("<li>");
                            treeHtml.Append(func(item));
                            treeHtml.Append("<ul>");
                            treeHtml.Append(AddTree(item.id, trees, noFunc, func));
                            treeHtml.Append("</ul></li>");
                        }
                        else
                        {
                            item.isChild = false;
                            treeHtml.Append("<li>");
                            treeHtml.Append(noFunc(item));
                            treeHtml.Append("</li>");
                        }
                    }
                    return treeHtml.ToString();
                }
                public string Create(string id, Func<TreeNode, string> noFunc, Func<TreeNode, string> func)
                {
                    StringBuilder treeHtml = new StringBuilder();
                    treeHtml.Append("<ul>");
                    treeHtml.Append(AddTree(id, list, noFunc, func));
                    treeHtml.Append("</ul>");
                    return treeHtml.ToString();
                }
    
    
                private string func(string id)
                {
                    string name = "";
                   // list = list.Where(m=>m.id==id).ToList();
                    if (id != "")
                    {
                        name = list.Where(m => m.id == id).First().name;
                        string ids = list.Where(m => m.id == id).First().parentId;
                        name += "," + func(ids);
                    }
                    return name;
                }
                public string[] GetPath(string id)
                {
                    if (id == "")
                    {
                        return new string[] { list.Where(m => m.parentId == "").First().name };
                    }
                    int index = func(id).Length;
                    return func(id).Substring(0, index - 1).Split(',').Reverse().ToArray(); ;
                }
    
                public int Dethp(string id)
                {
                    return GetPath(id).Length;
                }
                public bool HasChild(string id)
                {
                    if (list.Select(m => m.parentId).Count(m => m == id) > 0)
                    {
                        return true;
                    }
                    return false;
                }
            }
            public class TreeNode
            {
                public string id;
                public string parentId;
                public string name;
                public string action;
                public string control;
                public string decript;
                public string url;
                public string icon;
    
                public bool isChild;
                public int depth;
            }
        }
    }
  • 相关阅读:
    使用ADO.NET2.0提升数据交互性能 DataSet 数据表
    AD域控制器所有使用的端口明细列表
    链接数据库 远程事务的处理方式
    根据权限动态生成菜单栏和工具栏
    FTP服务器配置(cmd中ftp命令)
    该操作未能执行,因为 OLE DB 提供程序SQLOLEDB无法启动分布式事务
    ChartLet GDI+中发生一般性错误
    SQL SERVER 2000用户sa 登录失败的解决办法
    .net实例:网站发布后,在IIS中浏览提示:无法找到该页...404错误。
    Winform 关闭应用程序
  • 原文地址:https://www.cnblogs.com/lifeOfIT/p/3358717.html
Copyright © 2020-2023  润新知