Ejemplo n.º 1
0
        /// <summary>
        /// 生成菜单树
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="parentMenu"></param>
        /// <param name="nodes"></param>
        private int ResolveMenuTree(List <SYS_MENU> menus, SYS_MENU parentMenu, TreeNodeCollection nodes)
        {
            var count = 0;

            foreach (var menu in menus.Where(m => m.ParentId == (parentMenu?.Id ?? 0)))
            {
                var node = new TreeNode();
                nodes.Add(node);
                count++;

                node.Text    = menu.Name;
                node.IconUrl = menu.ImageUrl;
                if (!string.IsNullOrEmpty(menu.NavigateUrl))
                {
                    node.EnableClickEvent = false;
                    node.NavigateUrl      = ResolveUrl(menu.NavigateUrl);
                    //node.OnClientClick = String.Format("addTab('{0}','{1}','{2}')", node.NodeID, ResolveUrl(menu.NavigateUrl), node.Text.Replace("'", ""));
                }

                if (menu.ParentId > 0)
                {
                    node.Leaf = true;

                    // 如果是叶子节点,但是不是超链接,则是空目录,删除
                    if (string.IsNullOrEmpty(menu.NavigateUrl))
                    {
                        nodes.Remove(node);
                        count--;
                    }
                }
                else
                {
                    //node.SingleClickExpand = true;

                    int childCount = ResolveMenuTree(menus, menu, node.Nodes);

                    // 如果是目录,但是计算的子节点数为0,可能目录里面的都是空目录,则要删除此父目录
                    if (childCount == 0 && string.IsNullOrEmpty(menu.NavigateUrl))
                    {
                        nodes.Remove(node);
                        count--;
                    }
                }
            }

            return(count);
        }
Ejemplo n.º 2
0
        private static List <SYS_MENU> InitMenus()
        {
            var menus = new List <SYS_MENU>();

            var dbMenus = SYS_MENU.Fetch(
                Sql.Builder
                .LeftJoin("SYS_ROLE_MENU")
                .On("SYS_MENU.Id = SYS_ROLE_MENU.MenuId")
                .Where("RoleId IN (@0)", Current.RoleIds.ToArray())
                .Where("SYS_MENU.Active = @0", true)
                .Where("SYS_ROLE_MENU.Active = @0", true)
                .OrderBy("SortIndex ASC")
                ).Distinct(new MenuComparer()).ToList();

            ResolveMenuCollection(dbMenus, 0, 0, ref menus);

            return(menus);
        }