ztree 获取最下级的子节点内容
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Ztree</title> <link href="zTreeStyle.css" rel="stylesheet" /> <script type="text/javascript" src="jquery-1.4.4.min.js"></script> <script type="text/javascript" src="jquery.ztree.all-3.5.min.js"></script> <script type="text/javascript"> //获取页面中的参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return ""; } var zNodes=[{"key":null,"Id":0,"Pid":-1,"Name":"测试平台","checked":false},{"key":null,"Id":1,"Pid":0,"Name":"模块1","checked":false},{"key":7,"Id":6,"Pid":1,"Name":"模块1-功能1","checked":true},{"key":null,"Id":7,"Pid":1,"Name":"模块1-功能2","checked":false},{"key":null,"Id":8,"Pid":1,"Name":"模块1-功能3","checked":false},{"key":null,"Id":9,"Pid":1,"Name":"模块1-功能4","checked":false},{"key":null,"Id":10,"Pid":1,"Name":"模块1-功能5","checked":false},{"key":null,"Id":2,"Pid":0,"Name":"模块2","checked":false},{"key":7,"Id":11,"Pid":2,"Name":"模块2-功能1","checked":true},{"key":null,"Id":12,"Pid":2,"Name":"模块2-功能2","checked":false},{"key":null,"Id":13,"Pid":2,"Name":"模块2-功能3","checked":false},{"key":null,"Id":14,"Pid":2,"Name":"模块2-功能4","checked":false},{"key":null,"Id":15,"Pid":2,"Name":"模块2-功能5","checked":false},{"key":null,"Id":3,"Pid":0,"Name":"模块3","checked":false},{"key":7,"Id":16,"Pid":3,"Name":"模块3-功能1","checked":true},{"key":null,"Id":17,"Pid":3,"Name":"模块3-功能2","checked":false},{"key":null,"Id":18,"Pid":3,"Name":"模块3-功能3","checked":false},{"key":null,"Id":19,"Pid":3,"Name":"模块3-功能4","checked":false},{"key":null,"Id":20,"Pid":3,"Name":"模块3-功能5","checked":false},{"key":null,"Id":4,"Pid":0,"Name":"模块4","checked":false},{"key":7,"Id":21,"Pid":4,"Name":"模块4-功能1","checked":true},{"key":null,"Id":22,"Pid":4,"Name":"模块4-功能2","checked":false},{"key":null,"Id":23,"Pid":4,"Name":"模块4-功能3","checked":false},{"key":null,"Id":24,"Pid":4,"Name":"模块4-功能4","checked":false},{"key":null,"Id":25,"Pid":4,"Name":"模块4-功能5","checked":false},{"key":null,"Id":5,"Pid":0,"Name":"模块5","checked":false},{"key":7,"Id":26,"Pid":5,"Name":"模块5-功能1","checked":true},{"key":null,"Id":27,"Pid":5,"Name":"模块5-功能2","checked":false},{"key":null,"Id":28,"Pid":5,"Name":"模块5-功能3","checked":false},{"key":null,"Id":29,"Pid":5,"Name":"模块5-功能4","checked":false},{"key":null,"Id":30,"Pid":5,"Name":"模块5-功能5","checked":false}]; var setting = { check: { enable: true }, data: {//数据加载 keep: { parent: true, leaf: true }, simpleData: { enable: true, idKey: "Id", pIdKey: "Pid" }, key: { name: "Name" } } }; function Inint() {//初始化加载节点 $.ajax({ global: true, async: false, cache: false, dataType: "json", url: 'ajax/test.ashx', //通过一般处理程序生成数据 type: "post", data:{"roleId":GetQueryString("roleId")}, success: function (data) { zNodes = data; } }); }; $(function () { //Inint(); var treeObj =$.fn.zTree.init($("#powerTree"), setting, zNodes); treeObj.expandAll(true); }); jQuery().ready(function () { $("#btnTest").click(function () { var treeObj = $.fn.zTree.getZTreeObj("powerTree"); var nodes = treeObj.getCheckedNodes(true); var txt = ""; for (var i = 0; i < nodes.length; i++) { if (nodes[i].isParent) { //txt.replace(nodes[i].Name, ""); } else { txt += nodes[i].Id+"-"+nodes[i].Name + ","; } } txt = txt.replace("null,", ""); //替换选择父节点的id txt = txt.substring(0, txt.length - 1); alert(txt); //var strCheckNodes = JSON.stringify(checkNodes); }); }); </script> </head> <body> <div> <ul id="powerTree" class="ztree"> </ul> </div> <input type="button" name="btnTest" id="btnTest" value="测试节点" /> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace ajax { /// <summary> /// Sys_Role_PowerAllot 的摘要说明 /// </summary> public class Test : IHttpHandler { public void ProcessRequest(HttpContext context) { string roleId = context.Request["roleId"]; List<Sys_Module> list = new List<Sys_Module>(); for (int i = 0; i < 5; i++) { Sys_Module s = new Sys_Module(); s.Sys_Module_Id = i+1; s.sm_ParentId = i - i; s.sm_Name = "模块" + (i + 1); List<Sys_Function> funs = new List<Sys_Function>(); for (int j = 0; j < 5; j++) { Sys_Function fun = new Sys_Function(); if (j == 0) { fun.Sys_Function_Id = j + (i + 1) * 5 + 1; fun.sf_Description = s.sm_Name + "-功能" + (j + 1); fun.sf_AuthorityIndex = 7; funs.Add(fun); } fun.Sys_Function_Id = j + (i + 1) * 5 + 1; fun.sf_Description = s.sm_Name + "-功能" + (j + 1); funs.Add(fun); } s.Functions = funs; list.Add(s); } BindFunction(allModules, roleId); } } private string GetAuthorityStr(string nodes) { //string[] checkNodes = nodes.Split(','); //int[] intNodes = Array.ConvertAll<string, int>(checkNodes, delegate(string s) { return int.Parse(s); }); //ArrayList arrNodes = new ArrayList(intNodes); //arrNodes.Sort(); //int maxNodeId = Convert.ToInt32(arrNodes[arrNodes.Count - 1]); //string sr_AuthorityString = string.Empty; //List<string> list = new List<string>(); //for (int i = 0; i < maxNodeId+1; i++) //{ // list.Add("0"); //} //for (int i = 0; i < intNodes.Length; i++) //{ // list[intNodes[i]] = "1"; //} //sr_AuthorityString = string.Join("", list.ToArray()); //sr_AuthorityString = sr_AuthorityString.PadRight(1024, '0'); string[] checkNodes = nodes.Split(','); string sr_AuthorityString = string.Empty; sr_AuthorityString= sr_AuthorityString.PadRight(1024, '0'); for (int i = 0; i < checkNodes.Length; i++) { sr_AuthorityString = sr_AuthorityString.Remove(Convert.ToInt32(checkNodes[i])-1, 1).Insert(Convert.ToInt32(checkNodes[i])-1, "1"); } return sr_AuthorityString; } public string BindFunction(List<Sys_Module> modules,string roleId) { List<TreeData> funTree = new List<TreeData>(); TreeData treeRoot = new TreeData(); treeRoot.Id = 0; treeRoot.Pid = -1; treeRoot.Name = "测试平台"; char[] cauth = { }; //Sys_Role_Logic roleLogic = new Sys_Role_Logic(); //if (!string.IsNullOrEmpty(roleId)) //{ // Sys_Role role = roleLogic.GetModel(Convert.ToInt32(roleId)); // string authorityStr = role.sr_AuthorityString.Substring(0, role.sr_AuthorityString.LastIndexOf("1")+1); // //string authorityStr = "000000100100100000001000100000000000000"; // cauth= authorityStr.ToArray(); //} funTree.Add(treeRoot); for (int i = 0; i < modules.Count; i++) { AddNodes(funTree, modules[i], treeRoot.Id, cauth); } return JsonHelper.SerializeObject(funTree); } public List<TreeData> AddNodes(List<TreeData> trees, Sys_Module module, int pid, char [] authorityStr) { TreeData tree = new TreeData(); tree.Id = module.Sys_Module_Id; tree.Name = module.sm_Name; if (pid == 0) tree.Pid = module.sm_ParentId; else tree.Pid = pid; trees.Add(tree); foreach (Sys_Function fun in module.Functions) { trees.Add(AddFunNodes(fun, tree.Id,authorityStr)); } return trees; } public TreeData AddFunNodes(Sys_Function fun, int pid, char [] authorityStr) { TreeData tree = new TreeData(); tree.Id = fun.Sys_Function_Id; tree.Pid = pid; tree.Name = fun.sf_Description; tree.key = fun.sf_AuthorityIndex; int funAuthorityIndex = Convert.ToInt32(fun.sf_AuthorityIndex - 1); if (funAuthorityIndex < authorityStr.Length) { if (authorityStr[funAuthorityIndex] == '1') { tree.@checked = true; } else { tree.@checked = false; } } return tree; } public bool IsReusable { get { return false; } } } public class TreeData { public int? key { get; set; } public int Id { get; set; } public int? Pid { get; set; } public string Name { get; set; } public bool @checked { get; set; } } public class Sys_Function { public int Sys_Function_Id{get;set;} public string sf_Description{get;set;} public int sf_AuthorityIndex{get;set;} } public class Sys_Module { public int Sys_Module_Id{get;set;} public int sm_ParentId{get;set;} public string sm_Name{get;set;} public List<Sys_Function> Functions{get;set;} } }
posted on 2015-05-15 16:20 lovezj9012 阅读(3299) 评论(0) 编辑 收藏 举报