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;
        }
    }
}

 

posted @ 2013-10-09 10:59  蓝亭赏月  阅读(1663)  评论(0编辑  收藏  举报