菜单管理跟上篇的组织结构实现方式差不多,所以不存在多大的问题。还是老样,先上图,再看代码。菜单管理界面
菜单管理添加:
菜单管理修改:
前端完整代码:
@section headerScripts{
<style type="text/css">
#grid {
margin-left: 10px;
margin-top: 10px;
}
</style> <script type="text/javascript"> var UrlMenuDataSource = '@Url.Action("MenuDataSource")';
var UrlParentNode = '@Url.Action("ParentNode")';
var UrlAddMenu = '@Url.Action("AddMenu")';
var UrlModifyMenu = '@Url.Action("ModifyMenu")';
var UrlDeleteMenu = '@Url.Action("DeleteMenu")'; $(function () {
$("#grid").ligerGrid({
columns: [
{ display: '页面名称', name: 'name', id: 'tree1', align: 'left', width: '30%' },
{ display: '排列序号', name: 'tindex', type: 'int', align: 'left', width: '30%' },
{ display: '访问地址', name: 'url', align: 'left', width: '30%' },
{ display: '页面ID', name: 'rid', hide: 'hide', width: '1%' },
{ display: '页面父ID', name: 'parentrid', hide: 'hide', width: '1%' }
],
width: '98%',
pageSizeOptions: [, , ],
height: '100%',
rowHeight: ,
headerRowHeight: ,
url: UrlMenuDataSource,
alternatingRow: true,
tree: { columnId: 'tree1' },
dataAction: 'local',
toolbar: {
items: [
{ text: '增加', click: AddMenu, img: '@Url.Content("~/Content/LigerUI/icons/add.gif")' },
{ line: true },
{ text: '修改', click: ModifyMenu, img: '@Url.Content("~/Content/LigerUI/icons/modify.gif")' },
{ line: true },
{ text: '删除', click: DeleteMenu, img: '@Url.Content("~/Content/LigerUI/icons/delete.gif")' }
]
}
}
); //添加
function AddMenu() { if (!window.addWin) {
window.addWin = $.ligerDialog.open({
target: $("#winAdd"),
height: ,
width: ,
title: "增加菜单",
isHidden: false
});
$("#txtParentNode").ligerComboBox(
{
url: UrlParentNode,
valueField: 'rid',
textField: 'name',
selectBoxWidth: ,
autocomplete: true,
width: ,
hideOnLoseFocus: true,
css: 'combo'
} ); $("#btnCancel").click(function () {
window.addWin.hide();
}); $("#btnConfirm").click(function () { var parentNode = $("#txtParentNode").ligerComboBox().getValue();
var pageName = $("#txtPageName").val();
var url = $("#txtUrl").val();
var index = $("#txtTIndex").val(); if (pageName == "" || url == "") {
alert("页面名称和访问地址不能为空!");
return;
} $.post(UrlAddMenu,
{ parentNode: parentNode, pageName: pageName, url: url, index: index },
function (data) {
if (data.result) {
alert("操作成功!");
$("#grid").ligerGrid().reload();
} else {
alert(data.msg);
}
});
});
} else {
window.addWin.show();
} }
//修改
function ModifyMenu() {
var ModifyDialog;
var grid = $("#grid").ligerGrid();
var row = grid.getSelectedRow();
if (row == null) {
alert("请选择一条数据");
return;
}
$("#winModify").data("name",row.name);
$("#winModify").data("tindex",row.tindex);
$("#winModify").data("url", row.url);
$("#winModify").data("rid", row.rid);
$("#winModify").data("parentrid", row.parentrid); if (!window.modifyWin) {
window.modifyWin = $.ligerDialog.open({
target: $("#winModify"),
height: ,
width: ,
title: "修改菜单"
}); $("#txtModifyPageName").val( $("#winModify").data("name"));
$("#txtIndex").val( $("#winModify").data("tindex"));
$("#txtModifyUrl").val($("#winModify").data("url"));
$("#txtRid").val($("#winModify").data("rid"));
$("#txtParentRid").val( $("#winModify").data("parentrid")); $("#btnModifyCancel").click(function () {
window.modifyWin.hide();
}); $("#btnModifyConfirm").click(function () { var pageName = $("#txtModifyPageName").val();
var index = $("#txtIndex").val();
var url = $("#txtModifyUrl").val();
var rid = $("#txtRid").val();
var parentrid = $("#txtParentRid").val(); if (pageName == "" || url == "") {
alert("页面名称和访问地址不能为空");
return;
} $.post(UrlModifyMenu, { pageName: pageName, index: index, url: url, rid: rid, parentrid: parentrid }, function (data) {
if (data.result) {
alert("操作成功!");
$("#grid").ligerGrid().reload();
} else {
alert(data.msg);
}
});
});
} else {
$("#txtModifyPageName").val($("#winModify").data("name"));
$("#txtIndex").val($("#winModify").data("tindex"));
$("#txtModifyUrl").val($("#winModify").data("url"));
$("#txtRid").val($("#winModify").data("rid"));
$("#txtParentRid").val($("#winModify").data("parentrid"));
window.modifyWin.show();
} }
//删除
function DeleteMenu() {
var grid = $("#grid").ligerGrid();
var row = grid.getSelectedRow();
if (row == null) {
alert("请选择一条数据");
return;
}
if (confirm("是否确定删除?")) {
$.post(UrlDeleteMenu, { rid: row.rid }, function (data) {
if (data.result) {
alert("删除成功!");
$("#grid").ligerGrid().reload();
} else {
alert(data.msg);
}
});
} }
});
</script>
} <div id="grid"></div> <div id="winAdd" style="display: none;">
<table class="tb" style="height: 170px;">
<tr class="tr">
<td class="td">父级:</td>
<td>
<input id="txtParentNode" /></td>
</tr>
<tr class="tr">
<td class="td">页面名称:</td>
<td>
<input id="txtPageName" type="text" /></td>
</tr>
<tr class="tr">
<td class="td">访问地址:
</td>
<td>
<input id="txtUrl" type="text" />
</td> </tr>
<tr class="tr">
<td class="td">排列序号:
</td>
<td>
<input id="txtTIndex" type="text" />
</td> </tr>
<tr class="tr">
<td colspan="">
<button id="btnConfirm" class="ui-button">确定</button>
<button id="btnCancel" class="ui-button">取消</button>
</td>
</tr>
</table>
</div> <div id="winModify" style="display: none;">
<table class="tb" style="height: 170px;">
<tr class="tr">
<td class="td">页面名称:</td>
<td>
<input id="txtModifyPageName" type="text" /></td>
</tr>
<tr class="tr">
<td class="td">排列序号:</td>
<td>
<input id="txtIndex" /></td>
</tr>
<tr class="tr">
<td class="td">访问地址:
</td>
<td>
<input id="txtModifyUrl" type="text" />
<input id="txtRid" type="hidden" />
<input id="txtParentRid" type="hidden" />
</td> </tr>
<tr class="tr">
<td colspan="">
<button id="btnModifyConfirm" class="ui-button">确定</button>
<button id="btnModifyCancel" class="ui-button">取消</button>
</td>
</tr>
</table>
</div>
后端完整代码:
public class MenuController : Controller
{
//
// GET: /Menu/ private IMenuRepository _menuRepository; public MenuController(IMenuRepository menuRepository)
{ this._menuRepository = menuRepository;
} public ActionResult Index()
{
return View();
} public JsonResult MenuDataSource()
{ var data = _menuRepository.GetMenuParentsNode().ToList(); var list = new List<dynamic>(); foreach (var item in data)
{
var children = _menuRepository.GetMenuChildrenNodeByParentId(item.rid).ToList(); if (children == null || children.Count() == )
{
list.Add(new
{
name = item.name,
tindex = item.tindex,
url = item.url,
rid=item.rid,
parentrid = item.parentrid
});
}
else
{
list.Add(new
{
name = item.name,
tindex = item.tindex,
url = item.url,
rid = item.rid,
parentrid = item.parentrid,
children = children.Select(m => new {
name = m.name,
tindex = m.tindex,
url = m.url,
rid = m.rid,
parentrid = m.parentrid
})
});
}
} return Json(new
{
Rows = list,
Total = list.Count()
}, JsonRequestBehavior.AllowGet); } public JsonResult ParentNode()
{ var data = _menuRepository.GetMenuParentsNode().ToList(); return Json(data, JsonRequestBehavior.AllowGet);
} public JsonResult AddMenu(int? parentNode, string pageName, string url,int?index)
{
var check = _menuRepository.GetMenuByCondition(pageName, url); if (check.Count() > )
{
return Json(new { result = false, msg = "修改失败,已存在相同的菜单!" }, JsonRequestBehavior.AllowGet);
}
var menu = new t_resources()
{
name = pageName,
url = url,
parentrid = parentNode,
tindex=index
}; try
{
var result= _menuRepository.AddMenu(menu);
if (result)
{
return Json(new { result = true, msg = "" }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { result = false, msg = "操作失败!" }, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
return Json(new { result = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
}
} public JsonResult DeleteMenu(int rid)
{
var menu = new t_resources()
{
rid=rid
}; try
{
var result = _menuRepository.DeleteMenu(menu);
if (result)
{
return Json(new { result = true, msg = "" }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { result = false, msg = "操作失败!" }, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
return Json(new { result = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
}
} public JsonResult ModifyMenu(string pageName, int? index, string url, int rid, int? parentrid)
{
var check = _menuRepository.GetMenuByCondition(pageName,url); if (check.Count() > ) {
return Json(new { result = false, msg = "修改失败,已存在相同的菜单!" }, JsonRequestBehavior.AllowGet);
} var menu = new t_resources()
{
name=pageName,
tindex=index,
url=url,
rid=rid,
parentrid=parentrid
}; try
{
var result = _menuRepository.UpdateMenu(menu);
if (result)
{
return Json(new { result = true, msg = "" }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { result = false, msg = "操作失败!" }, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
return Json(new { result = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
}