<!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>
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 = ; i < ; i++)
{
Sys_Module s = new Sys_Module();
s.Sys_Module_Id = i+;
s.sm_ParentId = i - i;
s.sm_Name = "模块" + (i + );
List<Sys_Function> funs = new List<Sys_Function>();
for (int j = ; j < ; j++)
{
Sys_Function fun = new Sys_Function();
if (j == )
{
fun.Sys_Function_Id = j + (i + ) * + ;
fun.sf_Description = s.sm_Name + "-功能" + (j + );
fun.sf_AuthorityIndex = ;
funs.Add(fun);
}
fun.Sys_Function_Id = j + (i + ) * + ;
fun.sf_Description = s.sm_Name + "-功能" + (j + ); 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(, '');
for (int i = ; i < checkNodes.Length; i++)
{
sr_AuthorityString = sr_AuthorityString.Remove(Convert.ToInt32(checkNodes[i])-, ).Insert(Convert.ToInt32(checkNodes[i])-, "");
} return sr_AuthorityString;
} public string BindFunction(List<Sys_Module> modules,string roleId)
{ List<TreeData> funTree = new List<TreeData>();
TreeData treeRoot = new TreeData();
treeRoot.Id = ;
treeRoot.Pid = -;
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 = ; 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 == )
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 - );
if (funAuthorityIndex < authorityStr.Length)
{
if (authorityStr[funAuthorityIndex] == '')
{
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;}
}
}