标签:
<div id="using_json"></div> <input type="button" value="保存权限" class="btn btn-primary" id="saveper"/>
<script type="text/javascript"> $(document).ready(function() { $(‘#using_json‘).jstree({ "plugins": ["checkbox"], ‘checkbox‘: { "keep_selected_style": false, "tie_selection": true }, ‘core‘: { "multiple": true, "animation": 0, ‘data‘: @Html.Raw(ViewBag.aaa.ToString()) } }); $("#saveper").click(function() { var ouList = $(‘#using_json‘).jstree(‘get_selected‘); $.ajax({ type: ‘POST‘, url: "/Role/DistributionPer", dataType: ‘json‘, data: { "idlist": ouList.toString(), "roleId": @ViewBag.Id }, success: function(data) { AjaxCallBack(data); } }); }); }); </script>
树形结构MODEL
public class PerTreeView { public string text { get; set; } public string id { get; set; } public string icon { get; set; } public List<PerTreeView> children { get; set; } public State state { get; set; } }
权限树形结构初始化控制器
public ActionResult DistributionPer(int id) { ViewBag.Id = id; var perIdList = APIClient.AdminService.GetNodeIdListByRoleId(id); var a=GetPerTreeView(Permission.Where(s => s.Level != 0).ToList(), Permission, perIdList.Value.ToList()); var js=new JavaScriptSerializer(); ViewBag.aaa=js.Serialize(a); return View(); }
初始化JSON方法state=true时表示选中
private List<PerTreeView> GetPerTreeView(List<Permission> permission, List<Permission> permissionAll,List<int> idlist) { var treeViewList=new List<PerTreeView>(); foreach (var item in permission) { var treeView = new PerTreeView {text = item.Name,id = item.Id.ToString()}; if (permissionAll.Count(s => s.ParentId == item.Id) <= 0) { treeView.icon = ""; if (idlist.Contains(item.Id)) { treeView.state = new State() { selected = "true" }; } } else { treeView.children = GetPerTreeView(permissionAll.Where(s => s.ParentId == item.Id).ToList(), permissionAll, idlist); } treeViewList.Add(treeView); } return treeViewList; }
更新权限控制器
[HttpPost] public JsonResult DistributionPer(int roleId, string idlist) { if (string.IsNullOrWhiteSpace(idlist)) { return Json(new AjaxViewModel() { Code = AjaxCode.Error, Msg = "未选择权限" }); } var listId = idlist.Split(‘,‘).Select(item => item.ConvertToInt32()).ToList(); //排除之选一个的情况 if (listId.Count() <= 1) { return Json(new AjaxViewModel() { Code = AjaxCode.Error, Msg = "未选择权限" }); } var parIdList= listId.Select(item => { var firstOrDefault = Permission.FirstOrDefault(s => s.Id == item); return firstOrDefault?.ParentId ?? 0; }).ToList(); listId.AddRange(parIdList.Distinct()); var obj=APIClient.AdminService.UpdateRule(roleId, listId.ToArray()); if (obj.Code != 200) { return Json(new AjaxViewModel() { Code = AjaxCode.Error, Msg = "服务器内部错误" }); } return Json(new AjaxViewModel() { Code = AjaxCode.Ok, Msg = "修改完成", ReturnUrl = "/role/index" }); }
标签:
原文地址:http://www.cnblogs.com/harpz/p/4903930.html