zoukankan      html  css  js  c++  java
  • 把LIST递归成树形结构

    1.创建类

        public class Node
        {
            public bool leaf { get; set; }
            public int FolderID { get; set; }
            public string iconCls { get; set; }
            public bool editable { get; set; }
            public bool expanded { get; set; }
            public int ParentFolderID { get; set; }
            public string text { get; set; }
            public bool InheritPerm { get; set; }
            //public string CreateDate { get; set; }
            //public string CreateBy { get; set; }
            public Node[] children { get; set; }
        }

    2.从数据库获取数据转化成LIST

    3.递归构建树形结构

            public static string GetTree(List<Node> nodes)
            {
                var list = nodes.FindAll(a => a.ParentFolderID == 1);//最顶级的目录
                foreach (var node in list)
                {
                    if (node.text == "01.通讯录")
                    {
                        node.children = new Node[] { };//必须为空数组,不能为NULL,否则前端树节点会有错误
                    }
                    GetTree(node, nodes);
                }
                return JsonConvert.SerializeObject(list);
            }
    
    
            public static void GetTree(Node paretnNode, List<Node> nodes)
            {
                List<Node> nodelist = new List<Node>();
                foreach (var node in nodes)
                {
                    if (node.ParentFolderID == paretnNode.FolderID)
                    {
                        GetTree(node, nodes);
                        if (node.children == null)
                        {
                            node.children = new Node[] { };//必须为空数组,不能为NULL,否则前端树节点会有错误
                        }
                        nodelist.Add(node);
                        paretnNode.children = nodelist.ToArray();
                    }
                }
            }
  • 相关阅读:
    iOS block从零开始
    iOS 简单动画 序列帧动画
    iOS 简单动画 block动画
    IOS 简单动画 首尾式动画
    IOS 手势详解
    IOS block 循环引用的解决
    IOS GCD定时器
    IOS TextField伴随键盘移动
    IOS RunLoop面试题
    IOS RunLoop 常驻线程的实现
  • 原文地址:https://www.cnblogs.com/xielideboke/p/12161453.html
Copyright © 2011-2022 走看看