一、View:
1、定义输入控件
<input id="ParentId" name="ParentId">
2、绑定combotree
$(‘#ParentId‘).combotree({ url: "/Action/GetMenuTreeData/", method: "post", lines: true, required: true, onClick: function (node) { //自动生成排序号 $.post("/Action/GetMenuDisOrder/", { pId: node.id }, function (data) { $("#DisOrder").val(data); }); //自动生成当前层级 $.post("/Action/GetMenuDisLevel/", { pId: node.id }, function (data) { $("#DisLevel").val(data); }); } });
二、Controller:
[HttpPost] public ActionResult GetMenuTreeData() { //取从数据字典中取中Menu对应的ID号 var dic = _dictionaryService.Single(o => o.Name == "MENU" && o.IsDeleted == false); var menuId = dic.Id; //查询所有菜单信息 var menus = _actionService.Where(o => o.IsDeleted == false && o.OperationType == menuId && o.IsShow == true, o => o.DisOrder).ToList(); //获取根结点信息 RSCC.Model.TreeNode rootNode = _actionService.Single(o => o.ParentId == 0).ToNode(); //3、根据根结点对应的ID,查询其所有的子节点 rootNode.children = FillSonNodes(menus, rootNode.id); return Content("[" + operationContext.ToJson(rootNode) + "]"); }
/// <summary> /// 递归生成子节点集合 /// </summary> /// <param name="listPermission"></param> /// <param name="parentId"></param> /// <returns></returns> List<RSCC.Model.TreeNode> FillSonNodes(List<RSCC.Model.Action> list, int parentId) { List<RSCC.Model.TreeNode> sonNodes = null; //循环权限集合,查找子权限 foreach (RSCC.Model.Action m in list) { //查找到子权限 if (m.ParentId == parentId) { //实例化子节点集合 if (sonNodes == null) sonNodes = new List<RSCC.Model.TreeNode>(); //将子权限转成子节点 RSCC.Model.TreeNode sonNode = m.ToNode(); //将子节点加入子节点集合 sonNodes.Add(sonNode); //递归为子节点查找子节点集合 sonNode.children = FillSonNodes(list, sonNode.id); } } return sonNodes; }
三、显示效果:
时间: 2024-11-02 02:44:02