權限管理,ps:抄的領導的,比較粗糙,一切挺好的,就是沒有那個編輯用戶權限時,默認選中原來已有的權限節點。javascript
效果以下:html
cshtml代碼:java
1 @{ 2 ViewBag.Title = "學校後臺權限設置"; 3 Layout = "~/Views/Shared/_Layout2.cshtml"; 4 } 5 @section Head{ 6 <script src="@Url.Content("~/Scripts/BasicSchool_RightJS.js")" type="text/javascript"></script> 7 <script type="text/javascript"> 8 $(function () { 9 $("#provinceId").combobox({ 10 url: "/BasicSchool_Right/GetTeacher", 11 editable: false, 12 valueField: "id", 13 textField: "userName", 14 panelHeight: "auto" 15 }); 16 17 }); 18 </script> 19 } 20 <div region="center" style="width: 550px; height: 300px; padding: 1px; overflow-y: hidden"> 21 <div id="grid"> 22 </div> 23 </div> 24 <div id="rightSet-window" style="padding: 10px; background: #fff; border: 1px solid #ccc;width: 850px; height: 350px; padding: 10px"> 25 26 <ul id="rightSet-ul-window"></ul> 27 <a id="aRowId"></a> 28 <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveData()"> 保存</a> 29 </div> 30 31 <div id="search-window" title="搜索教師" style="width: 350px; height: 250px; padding: 10px"> 32 <div style="padding: 10px; background: #fff; border: 1px solid #ccc;"> 33 <form method="post" id="Form2"> 34 <table> 35 <tr> 36 <td style="text-align: right"> 37 教師: 38 </td> 39 <td> 40 <select id="provinceId" class="easyui-combogrid" name="provinceId" style="width: 200px;" 41 class="easyui-validatebox" required="true"> 42 </select> 43 </td> 44 </tr> 45 <tr> 46 <td style="text-align: right"> 47 教師名稱: 48 </td> 49 <td> 50 <input type="text" name="schoolName" id="schoolName" class="easyui-validatebox" 51 style="width: 200px;" maxlength="25" /> 52 </td> 53 </tr> 54 </table> 55 </form> 56 </div> 57 <div style="text-align: right; padding: 5px; vertical-align: bottom"> 58 <a href="javascript:void(0)" onclick="SearchOK()" id="btn-search" icon="icon-ok">搜索</a> 59 <a href="javascript:void(0)" onclick="closeSearchWindow()" id="btn-search-cancel" 60 icon="icon-cancel">取消</a> 61 </div> 62 </div>
後臺Action代碼:node
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using TeachAid.SpecialCloudEduPlatform.BLL; 7 using TeachAid.SpecialCloudEduPlatform.Entity; 8 using System.Web.Script.Serialization; 9 using Newtonsoft.Json; 10 11 namespace TeachAid.SpecialCloudEduPlatform.Web.Controllers 12 { 13 public class TreeViewLocation 14 { 15 public TreeViewLocation() 16 { 17 children = new HashSet<TreeViewLocation>(); 18 } 19 public int id { get; set; } 20 public string text { get; set; } 21 public ICollection<TreeViewLocation> children { get; set; } 22 } 23 24 public class BasicSchool_RightController : Controller 25 { 26 BLLBasicMain_UserInfo userInfoBLL = new BLLBasicMain_UserInfo(); 27 BLLJsonHelper bjb = new BLLJsonHelper(); 28 29 public ActionResult Index() 30 { 31 return View(); 32 } 33 34 /// <summary> 35 /// 查詢學校教師信息6 36 /// </summary> 37 /// <returns></returns> 38 public ActionResult GetTeacher() 39 { 40 IList<BasicMainUserInfo> userList = userInfoBLL.GetUserListBySchoolAndRold(((BasicMainUserInfo)Session["UserInfoLogin"]).schoolID, 1); 41 JavaScriptSerializer json = new JavaScriptSerializer(); 42 string str = json.Serialize(userList); 43 return Content(str, "text/html;charset=UTF-8"); 44 } 45 46 /// <summary> 47 /// 教師信息列表 48 /// </summary> 49 /// <returns></returns> 50 public ActionResult GetList() 51 { 52 int pageIndex = int.Parse(Request.Form["page"].ToString()); 53 int pageSize = int.Parse(Request.Form["rows"].ToString()); 54 string biao = "View_BasicSchool_UserTeacherDetails"; 55 string zhid = "id,userName,RealName,pwd,types,schoolID,schoolName,bid,cId,basicMain_UserInfoID,basicMain_SchoolId,isDirector,titles,birthday,phone,sex,email,blogUrl,motto,photo,isActivation,addTime"; 56 string str = " schoolID=" + ((BasicMainUserInfo)Session["UserInfoLogin"]).schoolID + " and types=1 "; 57 string json = bjb.GetJSON(pageIndex, pageSize, biao, zhid, "id", "desc", str); 58 return Content(json, "text/html;charset=UTF-8"); 59 } 60 61 /// <summary> 62 /// 得到樹節點菜單 63 /// </summary> 64 /// <returns></returns> 65 public ActionResult GetTreeJson() 66 { 67 BLLBasicMainTreeView blltv = new BLLBasicMainTreeView(); 68 JavaScriptSerializer j = new JavaScriptSerializer(); 69 return Content(j.Serialize(blltv.GetTreeViewBySchoolId(((BasicMainUserInfo)Session["UserInfoLogin"]).schoolID).ToArray()), "text/html;charset=UTF-8"); 70 } 71 72 /// <summary> 73 /// 保存爲當前用戶分配的權限信息 74 /// </summary> 75 /// <param name="ids"></param> 76 /// <param name="schoolid"></param> 77 /// <returns></returns> 78 public ActionResult CreateRightSet(string ids, string uid) 79 { 80 Message msg = new Message(true, "保存信息成功!"); 81 BLLBasicMainTreeView bllMainTree = new BLLBasicMainTreeView(); 82 bllMainTree.InsertNodeForSchoolUsers(int.Parse(uid), ids); 83 return Content(JsonConvert.SerializeObject(msg), "text/html;charset=UTF-8"); 84 } 85 } 86 }
js代碼:ajax
1 // 全局變量 2 var grid; 3 var dlg_Edit; 4 var dlg_Edit_form; 5 var searchWin; 6 var searchForm; 7 var virpath = ""; //網站的虛擬目錄 如:/ShopManager 8 9 10 $(function () { 11 grid = $('#grid').datagrid({ 12 title: '權限分配', 13 iconCls: 'icon-save', 14 methord: 'post', 15 url: '/BasicSchool_Right/GetList/', 16 sortName: 'id', 17 sortOrder: 'desc', 18 idField: 'id', 19 pageSize: 20, 20 striped: true, //奇偶行是否區分 21 columns: [[ 22 { field: 'id', title: '編號', width: 100, sortable: true }, 23 { field: 'userName', title: '教師登陸名', width: 100, sortable: true }, 24 { field: 'RealName', title: '真實姓名', width: 100, sortable: true }, 25 { field: 'addTime', title: '添加時間', width: 120, sortable: true }, 26 {field: 'schoolID', title: '權限管理', width: 50, rowspan: 2, align: 'left', 27 formatter: function (value, rec) { 28 return '<a style="color:blue;cursor:pointer;" href="#" onclick="editRightSet()">編輯</a>'; 29 } 30 } 31 ]], 32 fit: true, 33 pagination: true, 34 rownumbers: true, 35 fitColumns: true, 36 singleSelect: false, 37 toolbar: [{ 38 text: '高級搜索', 39 iconCls: 'icon-search', 40 handler: OpensearchWin 41 }, '-', { 42 text: '全部', 43 iconCls: 'icon-search', 44 handler: showAll 45 }], onDblClickRow: function (rowIndex, rowData) { //datagrid 行雙擊事件 46 47 48 }, onClickRow: function (value, rec) { //datagrid 行雙擊事件 49 $('#aRowId').val(rec.id); // 給schoolid 影藏控件賦值 50 }, onHeaderContextMenu: function (e, field) { 51 e.preventDefault(); 52 if (!$('#tmenu').length) { 53 createColumnMenu(); 54 } 55 $('#tmenu').menu('show', { 56 left: e.pageX, 57 top: e.pageY 58 }); 59 } 60 61 }); 62 63 dlg_Edit = $('#rightSet-window').dialog({ 64 closed: true, 65 66 toolbar: [{ 67 text: '保存', 68 iconCls: 'icon-save', 69 handler: saveData 70 }, '-', { 71 text: '關閉', 72 iconCls: 'icon-no', 73 handler: function () { 74 dlg_Edit.dialog('close'); 75 } 76 }] 77 78 }); 79 80 81 dlg_Edit_form = dlg_Edit.find('form'); 82 83 $('#btn-search,#btn-search-cancel').linkbutton(); 84 searchWin = $('#search-window').window({ 85 iconCls: 'icon-search', 86 closed: true, 87 modal: true 88 }); 89 searchForm = searchWin.find('form'); 90 $('body').layout(); 91 }); 92 93 //設置權限 94 function editRightSet() { 95 var rightSetWin = $('#rightSet-window').window({ 96 title: "設置權限", 97 closed: true, 98 modal: true, 99 minimizable: false 100 }); 101 rightSetWin.window('open'); 102 GetRightTree(); 103 rightSetWin.form('clear'); 104 } 105 106 //得到權限設置 107 function GetRightTree() { 108 $('#rightSet-ul-window').tree({ 109 checkbox: true, 110 url: '/BasicSchool_Right/GetTreeJson', 111 //cascadeCheck: false, 選擇此節點不須要父節點 112 onClick: function (node) { 113 $(this).tree('toggle', node.target); 114 }, onContextMenu: function (e, node) { 115 e.preventDefault(); 116 $('#rightSet-ul-window').tree('select', node.target); 117 } 118 }); 119 } 120 121 122 function createColumnMenu() { 123 var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body'); 124 var fields = grid.datagrid('getColumnFields'); 125 for (var i = 0; i < fields.length; i++) { 126 $('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu); 127 } 128 tmenu.menu({ 129 onClick: function (item) { 130 if (item.iconCls == 'icon-ok') { 131 grid.datagrid('hideColumn', item.text); 132 tmenu.menu('setIcon', { 133 target: item.target, 134 iconCls: 'icon-empty' 135 }); 136 } else { 137 grid.datagrid('showColumn', item.text); 138 tmenu.menu('setIcon', { 139 target: item.target, 140 iconCls: 'icon-ok' 141 }); 142 } 143 } 144 }); 145 } 146 147 148 149 //保存用戶的權限節點 150 function saveData() { 151 var nodes = $('#rightSet-ul-window').tree('getChecked'); 152 var str = ''; 153 for (var i = 0; i < nodes.length; i++) { 154 if (str != '') str += ','; 155 str += nodes[i].id; 156 } 157 if (str.length > 0) { 158 $.ajax({ 159 url: virpath + '/BasicSchool_Right/CreateRightSet/?ids=' + str + '&uid=' + $('#aRowId').val(), 160 type: 'post', 161 error: function () { 162 Msgalert('錯誤', '失敗!', 'error'); 163 }, 164 success: function (re) { 165 Msgalert('提示', '保存成功!', '提示'); 166 dlg_Edit.dialog('close'); 167 } 168 }); 169 } else { 170 Msgalert('提示', '請選擇你要設置的模塊!', '提示'); 171 } 172 173 } 174 175 function showAll() { 176 grid.datagrid({ url: virpath + '/BasicSchool_Right/GetList/' }); 177 } 178 function OpensearchWin() { 179 searchWin.window('open'); 180 searchForm.form('clear'); 181 $('#Enable2').combobox('setValue', true); //默認設置開啓搜素 182 } 183 184 function SearchOK() { 185 var Name = $("#Name").val(); 186 var Id = $("#TypeId2").combobox("getValue"); 187 var bl = $('#Enable2').combobox('getValue'); //默認設置開啓搜素 188 var PId = $("#pid").val(); //ID 189 var Price = $("#Price").val(); //價格 190 // alert(Name + "==" + Id + "==" + MId + "==" + bl + "==" + CId+"=="+Code); 191 searchWin.window('close'); 192 grid.datagrid({ url: virpath + '/Admin/SeachProductInfo/', queryParams: { ProductName: Name, typeId: Id, ProductId: PId, RealPrice: Price, en: bl} }); 193 //通過測試queryParams參數能夠提交到後臺經過FormCollection獲取 也能夠Request["ProductName"]=?獲取 194 } 195 function closeSearchWindow() { 196 searchWin.window('close'); 197 }
Action裏調用的Bll裏的方法,有一點兒須要注意的就是:使用的遞歸方法,循環插入節點,返回節點列表json
BLL層代碼:app
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using TeachAid.SpecialCloudEduPlatform.Entity; 6 7 namespace TeachAid.SpecialCloudEduPlatform.BLL 8 { 9 public class BLLBasicMainTreeView 10 { 11 private List<BasicMainTreeView> models = new List<BasicMainTreeView>(); 12 public List<BasicMainTreeView> GetTreeView() 13 { 14 DAL.DALBasicMainTreeView dalView = new DAL.DALBasicMainTreeView(); 15 models = dalView.GetTreeView(); 16 List<BasicMainTreeView> modp = new List<BasicMainTreeView>(); 17 BasicMainTreeView modelParent = new BasicMainTreeView(); 18 modelParent.id = -1; 19 modelParent.text = "權限設置"; 20 GetTree(0, modelParent); 21 modp.Add(modelParent); 22 return modp; 23 } 24 private void GetTree(int parentID, BasicMainTreeView moParent) 25 { 26 List<BasicMainTreeView> modelInfo = models.Where(Model => Model.parentID == parentID).ToList(); 27 foreach (BasicMainTreeView model in modelInfo) 28 { 29 BasicMainTreeView mo = new BasicMainTreeView(); 30 mo = model; 31 moParent.children.Add(mo); 32 GetTree(mo.id, mo); 33 } 34 } 35 36 /// <summary> 37 /// 根據學校ID,查詢該學校的全部權限節點 38 /// </summary> 39 /// <param name="schoolId">學校id</param> 40 /// <returns></returns> 41 public List<BasicMainTreeView> GetTreeViewBySchoolId(int schoolId) 42 { 43 DAL.DALBasicMainTreeView treeDAL = new DAL.DALBasicMainTreeView(); 44 models = treeDAL.GetTreeViewBySchoolId(schoolId); 45 List<BasicMainTreeView> treeList = new List<BasicMainTreeView>(); 46 BasicMainTreeView treeParent = new BasicMainTreeView(); 47 treeParent.id = -1; 48 treeParent.text = "權限設置"; 49 GetTree(0, treeParent); 50 treeList.Add(treeParent); 51 return treeList; 52 } 53 54 /// <summary> 55 /// 56 /// </summary> 57 /// <param name="schoolId">學校ID</param> 58 /// <param name="nodeID">權限節點 例如:1,2,3,4......</param> 59 public void InsertNodeID(int schoolId, string nodeID) 60 { 61 DAL.DALBasicMainTreeView dalView = new DAL.DALBasicMainTreeView(); 62 dalView.InsertNodeID(schoolId, nodeID); 63 } 64 65 66 /// <summary> 67 /// 爲學校用戶分配權限節點 68 /// </summary> 69 /// <param name="userId">用戶id</param> 70 /// <param name="nodeId">節點id</param> 71 public void InsertNodeForSchoolUsers(int userId, string nodeId) 72 { 73 DAL.DALBasicMainTreeView dalView = new DAL.DALBasicMainTreeView(); 74 dalView.InsertNodeForSchoolUsers(userId, nodeId); 75 } 76 } 77 }
方法:ide
private void GetTree(int parentID, BasicMainTreeView moParent)
{
List<BasicMainTreeView> modelInfo = models.Where(Model => Model.parentID == parentID).ToList();
foreach (BasicMainTreeView model in modelInfo)
{
BasicMainTreeView mo = new BasicMainTreeView();
mo = model;
moParent.children.Add(mo);
GetTree(mo.id, mo);
}
}裏的 models.Where(Model => Model.parentID == parentID).ToList();剛開始挺讓我費解的[ps:我太笨了,,] 有大仙兒網友說:就相似於:select * from model where model.parentID=parentID 查詢條件是parentIDpost
千萬別忘了。。。測試