系列目錄css
彷佛咱們須要更多的模塊了,咱們有一個樣例程序,能夠幫助咱們之後的系統開發作不少對照,咱們稍後還有系統日誌和系統異常的記錄,這時瀏覽發生了困難,咱們這一節來完成一個你們比較喜歡的東西吧,系統菜單欄,咱們系統左邊預留了一個位置,那裏存放菜單,菜單在這裏主要能夠分爲兩種,
1. outlook模式,能夠用easyui的accordion,作起來能夠很漂亮,博客園不少人寫的系統,我都看到了用這個,菜單前面還能夠放菜單圖標,這裏給你們看一個效果(這是J-UI富客戶端框架的菜單欄)優勢是漂亮啊,缺點也很明顯,只有兩層結構,系統龐大,菜單太多,顯示就很麻煩了,不夠用和很差看,accordion配合tree看起來很繁瑣,本人比較喜歡簡潔的東西html
accordion: accordion和tree結合:
2. tree模式,樹結構,缺點是簡潔,沒有視角上的享受,優勢是無限級別,ajax異步獲取,速度快。有不少樹的jquery插件,可使用easyui的tree,我這裏不用easyui的tree,我下載一個比較輕巧的jquery tree插件--wdtree這個插架很是輕巧,支持異步前端
動手吧!下載wdtree插件,我這裏提供下載,我之前下載的wdtree不是很好用(不知道如今有沒有新版本),可是我從新對這個插件進行修復,很符合菜單的操做,
wdtree修復版下載 node
解壓後,把js文件放到Scripts目錄下,把樣式放到Content目錄下jquery
咱們彷佛要建立數據庫的菜單表,建表SQL語句ajax
USE [DB] GO /****** Object: Table [dbo].[SysModule] Script Date: 11/19/2013 22:11:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[SysModule]( [Id] [varchar](50) NOT NULL, --id [Name] [varchar](200) NOT NULL, --模塊名稱 [EnglishName] [varchar](200) NULL, --英文名稱(防止未來國際化) [ParentId] [varchar](50) NULL,--上級ID這是一個tree [Url] [varchar](200) NULL,--連接 [Iconic] [varchar](200) NULL,--圖標,用於連接圖標,或tab頁圖標 [Sort] [int] NULL,--排序 [Remark] [varchar](4000) NULL,--說明 [State] [bit] NULL, --狀態 [CreatePerson] [varchar](200) NULL, --建立人 [CreateTime] [datetime] NULL,--建立事件 [IsLast] [bit] NOT NULL, --是不是最後一項 [Version] [timestamp] NULL, --版本 CONSTRAINT [PK_SysModule] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[SysModule] WITH NOCHECK ADD CONSTRAINT [FK_SysModule_SysModule] FOREIGN KEY([ParentId]) REFERENCES [dbo].[SysModule] ([Id]) GO ALTER TABLE [dbo].[SysModule] NOCHECK CONSTRAINT [FK_SysModule_SysModule] GO
在EF中更新咱們表模型,不知道怎麼更新的跳到第四講數據庫
執行數據庫命令,插入一些數據json
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('0','頂級菜單','Root','0','','',0,'',1,'Administrator','10 1 2012 12:00AM',0,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('BaseSample','模板樣例','Sample by Ajax','SampleFile','SysSample','',0,'',1,'Administrator',NULL,1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('Document','個人桌面','Start','PersonDocument','Home/Doc','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('Info','個人資料','Info','PersonDocument','Home/Info','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('InfoHome','主頁','Home','Information','MIS/Article','',1,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('Information','信息中心','Information','OA','','',0,'',1,'Administrator','10 1 2012 12:00AM',0,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('ManageInfo','管理中心','Manage Article','Information','MIS/ManageArticle','',4,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('ModuleSetting','模塊維護','Module Setting','RightManage','SysModule','',100,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('MyInfo','個人信息','My Article','Information','MIS/MyArticle','',2,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('PersonDocument','我的中心','Person Center','0','','',2,'',1,'Administrator','10 1 2012 12:00AM',0,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('RightManage','權限管理','Authorities Management','0','','',4,'',1,'Administrator','10 1 2012 12:00AM',0,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('RoleAuthorize','角色權限設置','Role Authorize','RightManage','SysRight','',6,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('RoleManage','角色管理','Role Manage','RightManage','SysRole','',2,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SampleFile','開發指南樣例','SampleFile','0','SysSample','',1,'',1,'Administrator',NULL,0,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemConfig','系統配置','System Config','SystemManage','SysConfig','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemExcepiton','系統異常','System Exception','SystemManage','SysException','',2,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemJobs','系統任務','System Jobs','TaskScheduling','Jobs/Jobs','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemLog','系統日誌','System Log','SystemManage','SysLog','',1,'',1,'Administrator','10 1 2012 12:00AM',1,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemManage','系統管理','System Management','0','','',3,'',1,'Administrator','10 1 2012 12:00AM',0,NULL) INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('UserManage','系統管理員','User Manage','RightManage','SysUser',NULL,1,NULL,1,'Administrator','10 1 2012 12:00AM',1,NULL)
咱們表裏有數據了
回到前端頁面,引入wdtree js及其樣式表框架
<head> ......... <script src="~/Scripts/jquery.tree.js"></script> <link href="~/Content/tree/css/tree.css" rel="stylesheet" /> </head>
添加如下代碼js代碼到home.js下面jquery插件
$(function () { var o = { showcheck: false, url: "/Home/GetTree", onnodeclick: function (item) { var tabTitle = item.text; var url = "../../" + item.value; var icon = item.Icon; if (!item.hasChildren) { addTab(tabTitle, url, icon); } else { $(this).parent().find("img").trigger("click"); } } } $.post("/Home/GetTree", { "id": "0" }, function (data) { if (data == "0") { window.location = "/Account"; } o.data = data; $("#RightTree").treeview(o); }, "json"); });
建立Home的BLL層和DAL層
using System; using System.Collections.Generic; using System.Linq; using System.Text; using App.Models; namespace App.IBLL { public interface IHomeBLL { List<SysModule> GetMenuByPersonId(string moduleId); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.Unity; using App.IBLL; using App.Models; using App.IDAL; namespace App.BLL { public class HomeBLL:IHomeBLL { [Dependency] public IHomeRepository HomeRepository { get; set; } public List<SysModule> GetMenuByPersonId(string moduleId) { return HomeRepository.GetMenuByPersonId(moduleId); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using App.Models; namespace App.IDAL { public interface IHomeRepository { List<SysModule> GetMenuByPersonId(string moduleId); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using App.Models; using App.IDAL; namespace App.DAL { public class HomeRepository : IHomeRepository,IDisposable { public List<SysModule> GetMenuByPersonId(string moduleId) { using (DBContainer db = new DBContainer()) { var menus = ( from m in db.SysModule where m.ParentId == moduleId where m.Id != "0" select m ).Distinct().OrderBy(a=>a.Sort).ToList(); return menus; } } public void Dispose() { } } }
修改HomeController的代碼
using App.IBLL; using App.Models; using Microsoft.Practices.Unity; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace App.Admin.Controllers { public class HomeController : Controller { // // GET: /Home/ [Dependency] public IHomeBLL homeBLL { get; set; } public ActionResult Index() { return View(); } /// <summary> /// 獲取導航菜單 /// </summary> /// <param name="id">所屬</param> /// <returns>樹</returns> public JsonResult GetTree(string id) { List<SysModule> menus = homeBLL.GetMenuByPersonId(id); var jsonData = ( from m in menus select new { id = m.Id, text = m.Name, value = m.Url, showcheck = false, complete = false, isexpand = false, checkstate = 0, hasChildren = m.IsLast ? false : true, Icon = m.Iconic } ).ToArray(); return Json(jsonData, JsonRequestBehavior.AllowGet); } } }
別忘記容器的注入App.Core下的
using System; using System.Collections.Generic; using System.Linq; using System.Web; using App.BLL; using App.DAL; using App.IBLL; using App.IDAL; using Microsoft.Practices.Unity; namespace App.Core { public class DependencyRegisterType { //系統注入 public static void Container_Sys(ref UnityContainer container) { container.RegisterType<ISysSampleBLL, SysSampleBLL>();//樣例 container.RegisterType<ISysSampleRepository, SysSampleRepository>(); container.RegisterType<IHomeBLL, HomeBLL>(); container.RegisterType<IHomeRepository, HomeRepository>(); } } }
回到前端,預覽如下效果,這一節代碼比較多,你們只要放進去,代碼我都有註釋,看下就知道了
咱們終於有菜單欄了,距離成功又進了一步,若是你有本身的菜單欄想法,那麼跳過這一節的內容。