zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    verdi issues on license
    geci
    组合数据类型练习
    熟悉常用的Linux操作
    1.大数据概述
    c语言文法分析
    词法分析器#include<stdio.h> #include<string.h> #include<iostream.h> char prog[80],token[8]; char ch; int syn,p,m=0,n,row,sum=0; char *rwtab[6]={"begin","if","then","while","do","end"
    关于编译原理
    可变参数
    函数和指针
  • 原文地址:https://www.cnblogs.com/lifeOfIT/p/3358717.html
Copyright © 2011-2022 走看看