zoukankan      html  css  js  c++  java
  • jsTree简单使用说明(适用于做权限)

       <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"
                });
    
            }
  • 相关阅读:
    HTML基础
    JVM内存和JVM调优(五)--分代垃圾回收详述
    JVM内存和JVM调优(四)--如何区分垃圾
    JVM内存和JVM调优(三)--基本GC垃圾回收算法
    JVM内存和JVM调优(二)--引用类型
    JVM内存和JVM调优(一)--堆栈概念
    isAssignableFrom和instanceof
    spring学习(三十九)--自定义注解
    SPRING学习(三十八)--SPRING集成MYBATIS之数据库连接池和多种数据源配置方式(三)
    SPRING学习(三十七)--SPRING集成MYBATIS(二)
  • 原文地址:https://www.cnblogs.com/harpz/p/4903930.html
Copyright © 2011-2022 走看看