LigerUI权限系统之菜单管理

  菜单管理跟上篇的组织结构实现方式差不多,所以不存在多大的问题。还是老样,先上图,再看代码。菜单管理界面

  菜单管理添加:

 

  菜单管理修改:

  前端完整代码:

  1 @section headerScripts{
  2     <style type="text/css">
  3         #grid {
  4             margin-left: 10px;
  5             margin-top: 10px;
  6         }
  7     </style>
  8 
  9     <script type="text/javascript">
 10 
 11         var UrlMenuDataSource = '@Url.Action("MenuDataSource")';
 12         var UrlParentNode = '@Url.Action("ParentNode")';
 13         var UrlAddMenu = '@Url.Action("AddMenu")';
 14         var UrlModifyMenu = '@Url.Action("ModifyMenu")';
 15         var UrlDeleteMenu = '@Url.Action("DeleteMenu")';
 16 
 17         $(function () {
 18             $("#grid").ligerGrid({
 19                 columns: [
 20                 { display: '页面名称', name: 'name', id: 'tree1', align: 'left', width: '30%' },
 21                 { display: '排列序号', name: 'tindex', type: 'int', align: 'left', width: '30%' },
 22                 { display: '访问地址', name: 'url', align: 'left', width: '30%' },
 23                 { display: '页面ID', name: 'rid', hide: 'hide', width: '1%' },
 24                 { display: '页面父ID', name: 'parentrid', hide: 'hide', width: '1%' }
 25                 ],
 26                 width: '98%',
 27                 pageSizeOptions: [10, 30, 50],
 28                 height: '100%',
 29                 rowHeight: 30,
 30                 headerRowHeight: 30,
 31                 url: UrlMenuDataSource,
 32                 alternatingRow: true,
 33                 tree: { columnId: 'tree1' },
 34                 dataAction: 'local',
 35                 toolbar: {
 36                     items: [
 37                    { text: '增加', click: AddMenu, img: '@Url.Content("~/Content/LigerUI/icons/add.gif")' },
 38                    { line: true },
 39                    { text: '修改', click: ModifyMenu, img: '@Url.Content("~/Content/LigerUI/icons/modify.gif")' },
 40                    { line: true },
 41                    { text: '删除', click: DeleteMenu, img: '@Url.Content("~/Content/LigerUI/icons/delete.gif")' }
 42                     ]
 43                 }
 44             }
 45             );
 46 
 47             //添加
 48             function AddMenu() {
 49               
 50                 if (!window.addWin) {
 51                     window.addWin = $.ligerDialog.open({
 52                         target: $("#winAdd"),
 53                         height: 260,
 54                         width: 400,
 55                         title: "增加菜单",
 56                         isHidden: false
 57                     });
 58                     $("#txtParentNode").ligerComboBox(
 59                        {
 60                            url: UrlParentNode,
 61                            valueField: 'rid',
 62                            textField: 'name',
 63                            selectBoxWidth: 135,
 64                            autocomplete: true,
 65                            width: 135,
 66                            hideOnLoseFocus: true,
 67                            css: 'combo'
 68                        }
 69 
 70                    );
 71 
 72                     $("#btnCancel").click(function () {
 73                         window.addWin.hide();
 74                     });
 75 
 76                     $("#btnConfirm").click(function () {
 77 
 78                         var parentNode = $("#txtParentNode").ligerComboBox().getValue();
 79                         var pageName = $("#txtPageName").val();
 80                         var url = $("#txtUrl").val();
 81                         var index = $("#txtTIndex").val();
 82 
 83                         if (pageName == "" || url == "") {
 84                             alert("页面名称和访问地址不能为空!");
 85                             return;
 86                         }
 87 
 88                         $.post(UrlAddMenu,
 89                             { parentNode: parentNode, pageName: pageName, url: url, index: index },
 90                             function (data) {
 91                                 if (data.result) {
 92                                     alert("操作成功!");
 93                                     $("#grid").ligerGrid().reload();
 94                                 } else {
 95                                     alert(data.msg);
 96                                 }
 97                             });
 98                     });
 99                 } else {
100                     window.addWin.show();
101                 }
102 
103             }
104             //修改
105             function ModifyMenu() {
106                 var ModifyDialog;
107                 var grid = $("#grid").ligerGrid();
108                 var row = grid.getSelectedRow();
109                 if (row == null) {
110                     alert("请选择一条数据");
111                     return;
112                 }
113                 $("#winModify").data("name",row.name);
114                 $("#winModify").data("tindex",row.tindex);
115                 $("#winModify").data("url", row.url);
116                 $("#winModify").data("rid", row.rid);
117                 $("#winModify").data("parentrid", row.parentrid);
118 
119 
120                 if (!window.modifyWin) {
121                     window.modifyWin = $.ligerDialog.open({
122                         target: $("#winModify"),
123                         height: 250,
124                         width: 400,
125                         title: "修改菜单"
126                     });
127                    
128                     $("#txtModifyPageName").val( $("#winModify").data("name"));
129                     $("#txtIndex").val( $("#winModify").data("tindex"));
130                     $("#txtModifyUrl").val($("#winModify").data("url"));
131                     $("#txtRid").val($("#winModify").data("rid"));
132                     $("#txtParentRid").val( $("#winModify").data("parentrid"));
133 
134                     $("#btnModifyCancel").click(function () {
135                         window.modifyWin.hide();
136                     });
137 
138                     $("#btnModifyConfirm").click(function () {
139 
140                         var pageName = $("#txtModifyPageName").val();
141                         var index = $("#txtIndex").val();
142                         var url = $("#txtModifyUrl").val();
143                         var rid = $("#txtRid").val();
144                         var parentrid = $("#txtParentRid").val();
145 
146                         if (pageName == "" || url == "") {
147                             alert("页面名称和访问地址不能为空");
148                             return;
149                         }
150 
151                         $.post(UrlModifyMenu, { pageName: pageName, index: index, url: url, rid: rid, parentrid: parentrid }, function (data) {
152                             if (data.result) {
153                                 alert("操作成功!");
154                                 $("#grid").ligerGrid().reload();
155                             } else {
156                                 alert(data.msg);
157                             }
158                         });
159                     });
160                 } else {
161                     $("#txtModifyPageName").val($("#winModify").data("name"));
162                     $("#txtIndex").val($("#winModify").data("tindex"));
163                     $("#txtModifyUrl").val($("#winModify").data("url"));
164                     $("#txtRid").val($("#winModify").data("rid"));
165                     $("#txtParentRid").val($("#winModify").data("parentrid"));
166                     window.modifyWin.show();
167                 }
168 
169             }
170             //删除
171             function DeleteMenu() {
172                 var grid = $("#grid").ligerGrid();
173                 var row = grid.getSelectedRow();
174                 if (row == null) {
175                     alert("请选择一条数据");
176                     return;
177                 }
178                 if (confirm("是否确定删除?")) {
179                     $.post(UrlDeleteMenu, { rid: row.rid }, function (data) {
180                         if (data.result) {
181                             alert("删除成功!");
182                             $("#grid").ligerGrid().reload();
183                         } else {
184                             alert(data.msg);
185                         }
186                     });
187                 }
188 
189             }
190         });
191     </script>
192 }
193 
194 <div id="grid"></div>
195 
196 <div id="winAdd" style="display: none;">
197     <table class="tb" style="height: 170px;">
198         <tr class="tr">
199             <td class="td">父级:</td>
200             <td>
201                 <input id="txtParentNode" /></td>
202         </tr>
203         <tr class="tr">
204             <td class="td">页面名称:</td>
205             <td>
206                 <input id="txtPageName" type="text" /></td>
207         </tr>
208         <tr class="tr">
209             <td class="td">访问地址:
210             </td>
211             <td>
212                 <input id="txtUrl" type="text" />
213             </td>
214 
215         </tr>
216         <tr class="tr">
217             <td class="td">排列序号:
218             </td>
219             <td>
220                 <input id="txtTIndex" type="text" />
221             </td>
222 
223         </tr>
224         <tr class="tr">
225             <td colspan="2">
226                 <button id="btnConfirm" class="ui-button">确定</button>
227                 <button id="btnCancel" class="ui-button">取消</button>
228             </td>
229         </tr>
230     </table>
231 </div>
232 
233 <div id="winModify" style="display: none;">
234     <table class="tb" style="height: 170px;">
235         <tr class="tr">
236             <td class="td">页面名称:</td>
237             <td>
238                 <input id="txtModifyPageName" type="text" /></td>
239         </tr>
240         <tr class="tr">
241             <td class="td">排列序号:</td>
242             <td>
243                 <input id="txtIndex" /></td>
244         </tr>
245         <tr class="tr">
246             <td class="td">访问地址:
247             </td>
248             <td>
249                 <input id="txtModifyUrl" type="text" />
250                 <input id="txtRid" type="hidden" />
251                  <input id="txtParentRid" type="hidden" />
252             </td>
253 
254         </tr>
255         <tr class="tr">
256             <td colspan="2">
257                 <button id="btnModifyConfirm" class="ui-button">确定</button>
258                 <button id="btnModifyCancel" class="ui-button">取消</button>
259             </td>
260         </tr>
261     </table>
262 </div>
View Code

  后端完整代码:

  1 public class MenuController : Controller
  2     {
  3         //
  4         // GET: /Menu/
  5 
  6         private IMenuRepository _menuRepository;
  7 
  8         public MenuController(IMenuRepository menuRepository)
  9         {
 10 
 11             this._menuRepository = menuRepository;
 12         }
 13 
 14         public ActionResult Index()
 15         {
 16             return View();
 17         }
 18 
 19         public JsonResult MenuDataSource()
 20         {
 21 
 22             var data = _menuRepository.GetMenuParentsNode().ToList();
 23 
 24             var list = new List<dynamic>();
 25 
 26             foreach (var item in data)
 27             {
 28                 var children = _menuRepository.GetMenuChildrenNodeByParentId(item.rid).ToList();
 29 
 30                 if (children == null || children.Count() == 0)
 31                 {
 32                     list.Add(new
 33                     {
 34                         name = item.name, 
 35                         tindex = item.tindex,
 36                         url = item.url,
 37                         rid=item.rid,
 38                         parentrid = item.parentrid
 39                     });
 40                 }
 41                 else
 42                 {
 43                     list.Add(new
 44                     {
 45                         name = item.name,
 46                         tindex = item.tindex,
 47                         url = item.url,
 48                         rid = item.rid,
 49                         parentrid = item.parentrid,
 50                         children = children.Select(m => new {
 51                             name = m.name,
 52                             tindex = m.tindex,
 53                             url = m.url,
 54                             rid = m.rid,
 55                             parentrid = m.parentrid
 56                         })
 57                     });
 58                 }
 59             }
 60 
 61             return Json(new
 62             {
 63                 Rows = list,
 64                 Total = list.Count()
 65             }, JsonRequestBehavior.AllowGet);
 66 
 67         }
 68 
 69         public JsonResult ParentNode()
 70         {
 71 
 72             var data = _menuRepository.GetMenuParentsNode().ToList();
 73 
 74             return Json(data, JsonRequestBehavior.AllowGet);
 75         }
 76 
 77         public JsonResult AddMenu(int? parentNode, string pageName, string url,int?index)
 78         {
 79             var check = _menuRepository.GetMenuByCondition(pageName, url);
 80 
 81             if (check.Count() > 1)
 82             {
 83                 return Json(new { result = false, msg = "修改失败,已存在相同的菜单!" }, JsonRequestBehavior.AllowGet);
 84             }
 85             var menu = new t_resources()
 86             {
 87                 name = pageName,
 88                 url = url,
 89                 parentrid = parentNode,
 90                 tindex=index
 91             };
 92 
 93             try
 94             {
 95               var result= _menuRepository.AddMenu(menu);
 96               if (result)
 97               {
 98                   return Json(new { result = true, msg = "" }, JsonRequestBehavior.AllowGet);
 99               }
100               else
101               {
102                   return Json(new { result = false, msg = "操作失败!" }, JsonRequestBehavior.AllowGet);
103               }
104             }
105             catch (Exception ex)
106             {
107                 return Json(new { result = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
108             }
109         }
110 
111         public JsonResult DeleteMenu(int rid)
112         {
113             var menu = new t_resources()
114             {
115                 rid=rid
116             };
117 
118             try
119             {
120                 var result = _menuRepository.DeleteMenu(menu);
121                 if (result)
122                 {
123                     return Json(new { result = true, msg = "" }, JsonRequestBehavior.AllowGet);
124                 }
125                 else
126                 {
127                     return Json(new { result = false, msg = "操作失败!" }, JsonRequestBehavior.AllowGet);
128                 }
129             }
130             catch (Exception ex)
131             {
132                 return Json(new { result = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
133             }
134         }
135 
136         public JsonResult ModifyMenu(string pageName, int? index, string url, int rid, int? parentrid)
137         {
138             var check = _menuRepository.GetMenuByCondition(pageName,url);
139 
140             if (check.Count() > 1) {
141                 return Json(new { result = false, msg = "修改失败,已存在相同的菜单!" }, JsonRequestBehavior.AllowGet);
142             }
143 
144             var menu = new t_resources()
145             {
146                 name=pageName,
147                 tindex=index,
148                 url=url,
149                 rid=rid,
150                 parentrid=parentrid
151             };
152 
153             try
154             {
155                 var result = _menuRepository.UpdateMenu(menu);
156                 if (result)
157                 {
158                     return Json(new { result = true, msg = "" }, JsonRequestBehavior.AllowGet);
159                 }
160                 else
161                 {
162                     return Json(new { result = false, msg = "操作失败!" }, JsonRequestBehavior.AllowGet);
163                 }
164             }
165             catch (Exception ex)
166             {
167                 return Json(new { result = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
168             }
169         }
170     }
View Code
posted @ 2013-08-02 21:48  Aulan  阅读(11006)  评论(0编辑  收藏  举报