Jquery Tree控件如何进行JSON数据绑定

一、 前端资源引用:
View Code
1 <link href="JS/Tree/tree.css" rel="stylesheet" type="text/css" />
2  <script src="JS/jquery-1.4.2.min.js" type="text/javascript"></script>
3  <script src="JS/Tree/TreeView.js" type="text/javascript"></script>
 
二、 HTML代码:
1 <div id="treeDiv"></div>
 
三、 创建数据请求页面(HttpHandler),aspx或者ashx文件都可以,我们需要做的就是将树形控件中的属性封装在匿名对象中,然后用JavaScriptSerialize类进行json数据格式的解析,具体代码如下:
//将TreeView中的属性封装成Object对象;

object NodeAdd(string sid, string stext, bool bshowcheck,
bool bPermissions, string svalue,
string scheckstate, bool bisexpand,
bool bcomplete, bool bhasChildren, object[] oChildNodes)
{
var obj
= new
{
id
= sid, //ID标识
text = stext, //显示的文本
showcheck = bshowcheck, //是否显示选择框
Permissions = bPermissions, //是否有权限
value = svalue, //显示文本对应的值
checkstate = scheckstate, //是否选中
isexpand = bisexpand, //是否展开
complete = bcomplete,
hasChildren
= bhasChildren, //是否有子节点
ChildNodes = oChildNodes //子节点内容
};
return obj;
}
 
1 //JSON序列化对象;
2  
3 string JsonSerialize(object o)
4 {
5 JavaScriptSerializer jSerializer = new JavaScriptSerializer();
6 return jSerializer.Serialize(o);
7 }
1 //判断是否有子节点
2  
3 bool HasChild(int parentid)
4 {
5 bool count =false;
6 DataSet ds = this.ds("select count(*) from [table name] where PID=" + parentid);
7 if (ds.Tables[0].Rows.Count>0)
8 {
9 count = true;
10 }
11 return count;

1 // 返回JSON字符串
2  
3 string BuilderJsonStr(int parentid)
4 {
5 string result = string.Empty;
6 int pid = parentid;
7 DataSet ds = this.ds("select * from [table name] where PID=" + parentid);
8 foreach (DataRow dr in ds.Tables[0].Rows)
9 {
10 pid = int.Parse(dr["ID"].ToString());
11 result += JsonSerialize(NodeAdd(dr["ID"].ToString(), dr["Name"].ToString(), true, true, dr["PID"].ToString(), "1", true, true,
12 HasChild(parentid),objectArray(pid)))+",";
13 }
14 return "["+result.TrimEnd(',')+"]";
15 }
16
17
18 // 递归生成object[]数组
19  
20 object[] objectArray(int parentid)
21 {
22 int pid = parentid;
23 DataSet ds = this.ds("select * from [table name] where PID=" + parentid);
24 object[] objects=new object[ds.Tables[0].Rows.Count];
25 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
26 {
27 DataRow dr = ds.Tables[0].Rows[i];
28 pid =int.Parse(dr["ID"].ToString());
29 objects[i] = NodeAdd(dr["ID"].ToString(), dr["Name"].ToString(), true, true, dr["PID"].ToString(),
30 "1", true, true,
31 HasChild(parentid), objectArray(pid));
32 }
33
34 return objects;
35 }


当然,我们将json数据准备好之后需要response出来,至于下面的输出json的代码可以根据需要添加在不同的地方(方法体最后或者方法调用的地方)

1 Response.ContentType = "application/json";
2 Response.Write(BuilderJsonStr(param));
3 Response.End();

四、 最后通过jquery ajax方法将json数据绑定到tree控件

1 <script type="text/javascript">
2 $(function() {
3 $.getJSON("GetTreeData.aspx", function(msg) {
4 var o = {
5 showcheck: true,
6 url: "GetTreeData.aspx",
7 datatype: "json",
8 onnodeclick: function(item) {
9 alert(item.id);
10 }
11 };
12 o.data = msg;
13 $('#treeDiv').treeview(o);
14 });
15 });
16  </script>

posted @ 2011-06-28 17:55  Bruceliu  阅读(2499)  评论(1)    收藏  举报