标签:
http://www.codeproject.com/Articles/996614/Angular-JS-Dynamic-Menu-Creation-using-MVC-and-WCF
新手翻译,看到一些好文,将其转过来,希望能对大家起到一点点的帮助.
1,为什么我们需要动态创建菜单:
如果我们只是需要创建一个只有几个页面的网站,那么用静态菜单就可以。
但是只果要开发一个大型Web应用的话,例如ERP Web应用.
在这种情况下,会有2个以上的开发者一起工作,而且页面可能会超过50-100个,这种情况下,用静态菜单将会是一个很大的挑战.
而且在开发中或之后,我们的客户可能会要求我们增加5个新的菜单,或移除掉一个菜单元素.
在使用静态菜单的情形下,这将会是一个很耗时的操作.
而且在开发大型Web项目时,比如ERP,我们需要基于用于角色来显示菜单,如果我们使用静态菜单的话,这将会一
件很难成.
为了解决这些情况,我们应该基于用于角色配置来创建菜单
谁应该管理这些菜单?
这是非常重要的部分,例如管理或超级用户可以 添加/编辑/删除 这些菜单或用户.
当管理员登录后,他能添加新菜单,编辑现有菜单和删除菜单元素。
这篇文章的关注点并不在于菜单管理而在于如何创建一个主菜单和菜单详情表,Insert一些示例菜单进我们的数据表,然后在我们的MVC Web页上使用AngularJS 和WCF Rest服务来动态的显示它.
这篇文章将会说胆:
1,如何创建一个WCF Rest服务和从数据库中查询数据
2,如何安装Angular JS Package进MVC 应用.
3,如何使用Angular JS建立一个动态菜单应用.
4,如何在Angular JS里使用WCS 服务显示动态菜单
注意:你首先必须安装Visual Studio 2013
Here we can see some basics and reference links for Windows Communication Foundation (WCF). WCF is a framework for building service-oriented applications.
Service-oriented application: Using this protocol the service can be shared and used over a network.
For example let‘s consider now we are working on a project and we need to create some common database function and those functions need to be used in multiple projects and the projects are in multiple places and connected via a network such as the internet.
In this case we can create a WCF service and we can write all our common database functions in our WCF service class. We can deploy our WCF in IIS and use the URL in our application to do DB functions. In the code part let‘s see how to create a WCF REST service and use it in our AngularJS application.
If you are interested in reading more details about WCF then kindly go to this link.
AngularJS
We might be be familiar with what Model, View and View Model (MVVM) is and what Model, View and Controller (MVC) is. AngularJS is a JavaScript framework that is purely based on HTML CSS and JavaScript.
Similar to the MVC and MVVM patterns AngularJS uses the Model, View and Whatever (MVW) pattern.
In our example I have used a Model, View and Service. In the code part let‘s see how to install and create AngularJS in our MVC application.
If you are interested in reading more details about WCF then kindly go to this link.
We will create a MenuMaster and MenuDetails table under the Database MenuDB.
Note: The MenuMaster and MenuDetail or the important tables which will be used to load our menu dynamically. We need to understand how to insert menu details to these tables to display our menu properly.
In this article I have display 3 Level hierarchical display of menu here you can see the 3 level hierarchical sample.
Here we can see 1 Level hierarchy-> Inventory;
2nd Level hierarchy-> inventory Details
3rd Level hierarchy-> Finished Goods Receipt and Finished Goods Issue.
Now let’s see how we create table relationship to create the master and detail menu.
Menu Master Table
1 Level hierarchy Insert
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Root‘,‘Inventory‘,‘Shanu‘,getdate()-23)
2 Level hierarchies Insert
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Inventory‘,‘INV001‘,‘Shanu‘,getdate()-23)
3 Level hierarchies Insert
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘INV001‘,‘FG001‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘INV001‘,‘FG002‘,‘Shanu‘,getdate()-23)
Menu Detail Table
1 Level hierarchy Insert
Insert into MenuDetails(Menu_ChildID,MenuName,MenuDisplayTxt,MenuFileName,
MenuURL,UserID,CreatedDate)
values(‘Inventory‘,‘Inventory‘,‘Inventory‘,‘Index‘,‘Inventory‘,
‘Shanu‘,getdate()-23)
2 Level hierarchies Insert
Insert into MenuDetails(Menu_ChildID,MenuName,MenuDisplayTxt,MenuFileName,
MenuURL,UserID,CreatedDate)
values(‘INV001‘,‘Inventory‘,‘Inventory Details‘,‘Index‘,‘Inventory‘,
‘Shanu‘,getdate()-23)
3 Level hierarchies Insert
Insert into MenuDetails(Menu_ChildID,MenuName,MenuDisplayTxt,MenuFileName,
MenuURL,UserID,CreatedDate)
values(‘FG001‘,‘FGIN‘,‘FG Receipt‘,‘FGIN‘,‘Inventory‘,‘Shanu‘,getdate()-43)
Insert into MenuDetails(Menu_ChildID,MenuName,MenuDisplayTxt,MenuFileName,
MenuURL,UserID,CreatedDate)
values(‘FG002‘,‘FGOUT‘,‘FG Issue‘,‘FGOUT‘,‘Inventory‘,‘Shanu‘,getdate()-13)
Here we can see Field used for Menu Detail I have used the fields
Here we can see field used for Menu Master I have used the fields
The following is the script to create a database, table and sample insert query. Run this script in your SQL Server. I have used SQL Server 2012.
-- =============================================
-- Author : Shanu
-- Create date : 2015-03-20
-- Description : To Create Database,Table and Sample Insert Query
-- Latest
-- Modifier : Shanu
-- Modify date : 2015-03-20
-- =============================================
--Script to create DB,Table and sample Insert data
USE MASTER
GO
-- 1) Check for the Database Exists .If the database is exist then drop and create new DB
IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = ‘MenuDB‘ )
DROP DATABASE MenuDB
GO
CREATE DATABASE MenuDB
GO
USE MenuDB
GO
-- 1) //////////// ToysDetails table
-- Create Table ToysDetails ,This table will be used to store the details like Toys Information
IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = ‘MenuMaster‘ )
DROP TABLE MenuMaster
GO
CREATE TABLE MenuMaster
(
Menu_ID int identity(1,1),
Menu_RootID VARCHAR(30) NOT NULL,
Menu_ChildID VARCHAR(30) NOT NULL,
UserID varchar(50),
CreatedDate datetime
CONSTRAINT [PK_MenuMaster] PRIMARY KEY CLUSTERED
(
[Menu_ID] ASC ,
[Menu_RootID] ASC,
[Menu_ChildID] 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
--delete from MenuMaster
-- Insert the sample records to the ToysDetails Table
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Root‘,‘Home‘,‘Shanu‘,getdate()-23)
--Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Home‘,‘Home‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Home‘,‘About‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Home‘,‘Contact‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Root‘,‘Masters‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Masters‘,‘ITM001‘,‘Shanu‘,getdate()-23)
--Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘ITM001‘,‘ITM001‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘ITM001‘,‘ITM002‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘ITM001‘,‘ITM003‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Masters‘,‘CAT001‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘CAT001‘,‘CAT001‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘CAT001‘,‘CAT002‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘CAT001‘,‘CAT003‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Root‘,‘Inventory‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘Inventory‘,‘INV001‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘INV001‘,‘FG001‘,‘Shanu‘,getdate()-23)
Insert into MenuMaster(Menu_RootID,Menu_ChildID,UserID,CreatedDate) values(‘INV001‘,‘FG002‘,‘Shanu‘,getdate()-23)
select * from MenuMaster
-- 1) END //
-- 2) Cart Details Table
IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = ‘MenuDetails‘ )
DROP TABLE MenuDetails
GO
CREATE TABLE MenuDetails
(
MDetail_ID int identity(