C# 樹形菜單的相關實現


在一些涉及到組織架構的場景, 比如ERP, OA系統中我們經常會需要用到樹的結構。自己開發的時候也遇到了此使用場景,總結一下以后就可以直接拿來用了。

 表設計如下:

 代碼如下:        

        public class Node
        {
            public int NodeId { get; set; }      //節點Id
            public int ParentId { get; set; }   //節點父級Id 如果為0則為根節點
            public string NodeName { get; set; }   //節點名
            public DateTime WriteTime { get; set; }  // 備注
        }

        public class TreeNode
        {
            public int NodeId { get; set; }      //節點Id
            public int ParentId { get; set; }   //節點父級Id 如果為0則為根節點
            public string NodeName { get; set; }   //節點名
            public DateTime WriteTime { get; set; }  //寫入時間
            public List<TreeNode> Children { get; set; }//子節點樹
        }        
        
        /// <summary>
        /// 獲取當前系統的結構樹,可能多個樹
        /// </summary>
        /// <returns></returns>
        public List<TreeNode> GetSysNodeTree()
        {
            using (MySqlHelper mySqlHelper = new MySqlHelper("MySqlTestDB"))
            {
                List<TreeNode> treeNodeList = new List<TreeNode>();
                //這一步數據庫查詢所有的記錄 (避免遞歸中多次查詢數據庫)
                List<Node> allNode = mySqlHelper.GetList_ExecuteSql<Node>("select * from tb_node");

                //遍歷所有的根節點
                var rootList = allNode.Where(s => s.ParentId == 0).ToList();
                foreach (var ent in rootList)
                {
                    TreeNode treeNode = new TreeNode();
                    treeNode.NodeId = ent.NodeId;
                    treeNode.NodeName = ent.NodeName;
                    treeNode.ParentId = ent.ParentId;
                    treeNode.Children = GetChildrenTree(ent.NodeId, allNode);
                    treeNodeList.Add(treeNode);
                }

                return treeNodeList;
            }
        }

        /// <summary>
        /// 獲取當前節點的節點樹
        /// </summary>
        /// <param name="curNodeId"></param>
        /// <param name="allNode"></param>
        /// <returns></returns>
        public List<TreeNode> GetChildrenTree(int curNodeId, List<Node> allNode)
        {
            if (allNode == null || allNode.Count <= 0)
                return new List<TreeNode>();

            List<TreeNode> TreeList = new List<TreeNode>();
            List<Node> children = allNode.Where(s => s.ParentId == curNodeId).ToList();
            foreach (var ent in children)
            {
                TreeNode treeNode = new TreeNode();
                treeNode.NodeId = ent.NodeId;
                treeNode.NodeName = ent.NodeName;
                treeNode.ParentId = ent.ParentId;
                treeNode.Children = GetChildrenTree(ent.NodeId, allNode);
                TreeList.Add(treeNode);
            }

            return TreeList;
        }

        /// <summary>
        /// 通過curNodeId 獲取自己以及遞歸下的所有子Node (用於刪除功能等)
        /// </summary>
        /// <param name="curNodeId"></param>
        /// <param name="allNode"></param>
        /// <returns></returns>
        public List<Node> GetRecurNodes(int curNodeId, List<Node> allNode)
        {
            if (curNodeId <= 0 || allNode==null)
            {
                return null;
            }
            List<Node> list = new List<Node>();
            if (allNode.FirstOrDefault((m=>m.NodeId==curNodeId))!=null)
                list.Add(allNode.FirstOrDefault((m => m.NodeId == curNodeId)));
            List<Node> firstLevelNodes = allNode.Where(m => m.ParentId == curNodeId).ToList();
            foreach (var node in firstLevelNodes)
            {
                list.AddRange(GetRecurNodes(node.NodeId, allNode));
            }

            return list;
        }             

然后前端拿到數據,同樣遞歸展示就OK了。

轉載注明出處-祥子


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM