C#
zTree.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
/// <summary>
/// zTree 的摘要說明
/// </summary>
public class zTree
{
#region 構造函數
public zTree()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
#endregion
public string GetModule()
{
string sql = " SELECT m.ID, m.Name, m.ParentID, m.Url, m.Ico FROM Module m ORDER BY m.Level, m.[Index] ";
DataTable dt = Common.GetTable(sql);
string treeJson = DtToJson(dt, " ParentID ", " 0 ", " ID ", " Name ").Substring( 12);
return treeJson;
}
// json處理
public static string DtToJson(DataTable dt, string pField, string pValue, string kField, string TextField)
{
StringBuilder sb = new StringBuilder();
string filter = String.Format( " {0}='{1}' ", pField, pValue); // 獲取頂級目錄.
DataRow[] drs = dt.Select(filter);
if (drs.Length < 1)
return "";
sb.Append( " ,\"children\":[ ");
foreach (DataRow dr in drs)
{
string pcv = dr[kField].ToString();
sb.Append( " { ");
sb.AppendFormat( " \"name\":\"{0}\", ", dr[TextField].ToString());
sb.AppendFormat( " \"id\":\"{0}\", ", dr[kField].ToString());
sb.AppendFormat( " \"pId\":\"{0}\", ", dr[kField].ToString());
sb.AppendFormat( " \"url\":\"{0}\", ", " http://www.baidu.com ");
sb.AppendFormat( " \"icon\":\"{0}\", ", "");
sb.AppendFormat( " \"open\":\"{0}\", ", true);
sb.AppendFormat( " \"checked\":\"{0}\" ", false);
sb.Append(DtToJson(dt, pField, pcv, kField, TextField).TrimEnd( ' , '));
sb.Append( " }, ");
}
if (sb.ToString().EndsWith( " , "))
{
sb.Remove(sb.Length - 1, 1);
}
sb.Append( " ] ");
return sb.ToString();
}
}
Common.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
/// <summary>
/// Class1 的摘要說明
/// </summary>
public class Common
{
public Common()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public static DataTable GetTable( string sql)
{
string strConn = @" Provider=SQLOLEDB;Data Source=127.0.0.1\SQLEXPRESS;Initial Catalog=a;User Id=sa;Password=123456; ";
DataSet ds = new DataSet();
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
if (conn.State != ConnectionState.Open)
conn.Open();
OleDbCommand comm = new OleDbCommand(sql, conn);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
da.Fill(ds);
if (conn.State != ConnectionState.Closed)
conn.Close();
}
dt = ds.Tables[ 0];
return dt;
}
}
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
/// <summary>
/// Class1 的摘要說明
/// </summary>
public class Common
{
public Common()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public static DataTable GetTable( string sql)
{
string strConn = @" Provider=SQLOLEDB;Data Source=127.0.0.1\SQLEXPRESS;Initial Catalog=a;User Id=sa;Password=123456; ";
DataSet ds = new DataSet();
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
if (conn.State != ConnectionState.Open)
conn.Open();
OleDbCommand comm = new OleDbCommand(sql, conn);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
da.Fill(ds);
if (conn.State != ConnectionState.Closed)
conn.Close();
}
dt = ds.Tables[ 0];
return dt;
}
}
Handler.ashx
<%@ WebHandler Language=
"
C#
" Class=
"
Handler
" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = " text/plain ";
string method = context.Request[ " action "];
switch (method)
{
case " GetModule ":
{
string a = context.Request.Params[ " id "]; // 參數為id
zTree zTree = new zTree();
context.Response.Write(zTree.GetModule());
break;
}
default:
break;
}
}
public bool IsReusable {
get {
return false;
}
}
}
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = " text/plain ";
string method = context.Request[ " action "];
switch (method)
{
case " GetModule ":
{
string a = context.Request.Params[ " id "]; // 參數為id
zTree zTree = new zTree();
context.Response.Write(zTree.GetModule());
break;
}
default:
break;
}
}
public bool IsReusable {
get {
return false;
}
}
}
aspx
<ul id=
"
treeDemo
"
class=
"
ztree
"></ul>
js
var setting = {
data: {
simpleData: {
enable: true
}
}
};
function createTree () {
var zNodes;
$.ajax({
url: ' Handler.ashx?action=GetModule ', // url action是方法的名稱
data: { id: " 11 " },
type: ' POST ',
dataType: " text ", // 可以是text,如果用text,返回的結果為字符串;如果需要json格式的,可是設置為json
ContentType: " application/json; charset=utf-8 ",
success: function(data) {
zNodes = data;
$.fn.zTree.init($( " #treeDemo "), setting, eval( ' ( ' + zNodes + ' ) '));
},
error: function(msg) {
alert( " 失敗 ");
}
});
}
$(document).ready(function() {
createTree();
});