26.加载角色数据【给角色:分配权限】
1.角色加载页(角色数据加载)
@{ Layout = null; } <!DOCTYPE html> <html> <head> <title>用户角色</title> <link href="~/EasyUI/themes/icon.css" rel="stylesheet" type="text/css" /> <link href="~/EasyUI/themes/default/easyui.css" rel="stylesheet" /> <script type="text/javascript" src="~/Scripts/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="~/EasyUI/jquery.easyui.min.js"></script> <script type="text/javascript" src="~/Scripts/jquery.msgProcess.js"></script> <script type="text/javascript"> //=====================1.加载数据====================== $(function () { $('#Windowp').window('close');//编辑默认关闭 $("#tbList").datagrid({ width: 'auto', height: 400, striped: true, singleSelect: true, url: '/admin/Role/index', loadMsg: '数据加载中请稍后……', pagination: true,//启用分页,默认每页10行 rownumbers: true,//显示页码,默认 提供 10 - 50 的页容量选择下拉框 pageSize: 10,//设置 页容量为 5 pageList: [10, 15, 20, 25, 30],//设置 页容量下拉框 fitColumns:true, columns: [[ { field: 'rIsShow', title: '显示', width:5,align: "center", formatter: function (value) { return value ? "√":"X"; } }, { field: 'rId', title: 'ID', width: 20, align: "center" }, { field: 'Ou_Department', title: '部门', width: 20, align: "center", formatter: function (value) { return value.depName } }, { field: 'rName', title: '角色名', width: 20, align: "center" }, { field: 'rIsDel', title: '是否删除', width: 20, align: "center", formatter: function (value) { switch (value.toString()) { case "false": return "否"; case "true": return "是"; } } }, { field: 'rRemark', title: '备注', width: 20, align: "center" }, ]], toolbar: [ { iconCls:'icon-edit_add', text: "分配权限", handler: providePermission //2.分配角色权限 } ], //设置选中行 onSelect: function (rowI, rD) { selectRowIndex = rowI;//1.1将中的行设置为全局变量 设置给全局变量 } }); initMoifyForm(); }) //1.将中的行设置为全局变量 var selectRowIndex = -1; //2.分配权限:角色权限 function providePermission() { //获取选中 的 角色数据 var roleData = $("#tbList").datagrid("getSelected"); if (roleData) { //调到权限分配页:拿角色id过去 $.get("/admin/role/getroleTrue/" + roleData.rId, null, function (jsobj) { if (jsobj.Statu) $.procAjaxData(jsobj); else $("#windowOper").html(jsobj); }); } else { $.alertMsg("请选中您要分配权限的角色数据", "系统提示"); } $("#windowOper").wimdow("open"); } </script> </head> <body> <table id="tbList" style="width:700px;height:250px"></table> <div id="Windowp" class="easyui-window" title="分配权限" style="width:600px; height:300px;" data-options="iconCls:'icon-save',modal:true,title:'分配权限'"><div id="windowOper" data-options="region:'center',style=" padding:10px;"> </div> <div data-options="region:'south',border:false," style="text-align:right;padding:5px;"> <a class="easyui-linkbutton" data-options="iconCls:'icon-save'," href="javascript:void(0)" onclick="javascript:alert('ok')">保存</a> <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="javascript:alert('cancel')">取消</a> </div></div> </body> </html>
getroleTrue.cshtml(权限分配页)
@model MODEL.ViewModel.RolePermissionTree <!--1.循环所有父菜单,为每个父菜单生成一棵树--> @for (int i = 0; i < Model.Allparent.Count; i++) { var per = Model.Allparent[i]; <ul id="tt @i" class="easyui-tree" style="display:inline-block;" data-options="checkbox:true,lines:true"> @*//checkbox="true"*@ <li> <span>@per.pName</span> <ul>
<!-- 2.生成菜单,从所有菜单结合中查询 属性当前循环 父菜单 的子菜单--> @foreach (var son in Model.AllPer.Where(p => p.pParent == per.pid).ToList()) {
<!-- 3.如果 当前子菜单 和用户菜单一样,则需设置为选中状态--> <li @if(Model.UserPer.Where(ur=>ur.pid==son.pid).Count()>0){<text>data-options="checked:true"</text>})> <span>@son.pName</span> </li> } </ul> </li> </ul> }
权限分配控制器action方法:getroleTrue。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using MVCOA.Helper; using MODEL.ViewModel; namespace MVCOA.Login.Admin { public class RoleController:Controller { #region 1. 加载角色数据 /// <summary> /// 角色 视图 /// </summary> /// <returns></returns> [HttpGet] public ActionResult index() { return View(); } [HttpPost] public ActionResult index(FormCollection form) { //获取页容量 int pageSize = int.Parse(Request.Form["rows"]); //获取请求的页面 int pageIndex = int.Parse(Request.Form["page"]); var list = OperateContext.Current.BLLSession.IOu_RoleBLL.GetPagedList(pageIndex, pageSize, p => p.rIsDel == false, p => p.rId).Select(p => p.ToPOCO()).ToList(); var rowcont = OperateContext.Current.BLLSession.IOu_RoleBLL.GetListBy(d => d.rIsDel == false).Count(); return Json(new MODEL.EasyUIModel.DataGridModel() { rows = list, total = rowcont }); } #endregion /// <summary> /// 3. 加载 所以的权限树[把对应的权限选中] /// </summary> /// <param name="id">角色id</param> /// <returns></returns> [HttpGet] public ActionResult getroleTrue(int id) { //1.获取已有的角色,2.获取所以的角色 //1.获取角色 权限 var listUserPer = OperateContext.Current.BLLSession.IOu_RolePermissionBLL.GetPermissionByRoleId(id); //2.获取所以权限 var listAllper= OperateContext.Current.BLLSession.IOu_PermissionBLL.GetListBy(b => b.pIsDel == false).ToList(); //3.获取父 权限集合 var listparentPer = (from p in listAllper where p.pParent == 1 && p.pIsDel == false select p).ToList(); return View(new RolePermissionTree() { UserPer = listUserPer, AllPer = listAllper, Allparent = listparentPer }); } } }
1.把获取角色 权限;2.获取所有权限;3.获取父 权限集合
封装到list集合中,便于调用,例如: return View(new RolePermissionTree() { UserPer = listUserPer, AllPer = listAllper, Allparent = listparentPer });
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MODEL.ViewModel { /// <summary> /// 角色权限树视图 实体 /// </summary> public class RolePermissionTree { /// <summary> ///某角色的权限 /// </summary> public List<MODEL.Ou_Permission> UserPer{get;set;} /// <summary> /// 系统中所有权限 /// </summary> public List<MODEL.Ou_Permission> AllPer { get; set; } /// <summary> /// 所以父权限 /// </summary> public List<MODEL.Ou_Permission> Allparent { get; set; } } }
运行效果图:
选中角色列,点击分配权限
已有的权限打勾。