html代码:
<input id="Series" name="Series" class="easyui-combotree" method=‘get‘/>
js代码: $(document).ready(function () { $(‘#Series‘).combotree({ //需要返回的json格式 //data: [ // { // "id": "222", "text": "黑色系列", // "children": [ // { "id": "280", "text": "黑1"}, // { "id": "282", "text": "黑2"} // ] // }, // { // "id": "225", "text": "白色系列", // "children": [{ "id": "281", "text": "白1" }, // { "id": "283", "text": "白2" } // ] // }], //请求数据的一般处理程序的路径 url: getRootPath() + ‘/handler/Page/Product/ProductClassify.ashx?action=MList‘, multiple: false,//是否有多选框 lines: true//选项前是否显示虚线 }); }); 一般处理程序:
<%@ WebHandler Language="C#" Class="ProductClassify" %> using System; using System.Web; using System.Data; using System.Text; using System.Web.Script.Serialization; public class ProductClassify : IHttpHandler { HttpContext Context = null; JiaSoft.BLL.Classify_BLL PClassiyList = new JiaSoft.BLL.Classify_BLL(); public void ProcessRequest (HttpContext context) { Context = context; string action = context.Request["action"].ToString(); responseAction(action); } private void responseAction(string action) { if (action.Equals("MList")) //获得多级列表 { InitComboTree(Context, PClassiyList); } } //初始化ComboTree数据 public void InitComboTree(HttpContext context, JiaSoft.BLL.Classify_BLL PClassiyList) { DataTable dt; System.Text.StringBuilder sb = new System.Text.StringBuilder(); //数据库设计名称、id、父类id 初始化先获取父类id=0的数据集,返回数据是DataTable类型 dt = PClassiyList.GetList(" Mtype=102 and ParentID=0 ").Tables[0]; string sbs =string.Empty; if (dt == null) { sbs= "[]"; } StringBuilder builder = new StringBuilder(); builder.Append("["); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { builder.Append("{"); builder.Append("\"id\":" + new JavaScriptSerializer().Serialize(dt.Rows[i]["ClassifyID"].ToString())); builder.Append(","); builder.Append("\"text\":" + new JavaScriptSerializer().Serialize(dt.Rows[i]["CodeName"].ToString())); builder.Append(","); //调用递归方法 builder = InitChild(builder, dt.Rows[i]["ClassifyID"].ToString(), PClassiyList); builder.Append("}"); if (i < (dt.Rows.Count - 1)) { builder.Append(","); } } } builder.Append("]"); sbs = builder.ToString(); Context.Response.Write(sbs); } //递归子类 public StringBuilder InitChild(StringBuilder builder, string parentID, JiaSoft.BLL.Classify_BLL PClassiyList) { DataTable dt = PClassiyList.GetList(" Mtype=102 and ParentID=" + parentID ).Tables[0]; if (dt == null) { builder.Append(""); } builder.Append("\"children\":["); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { builder.Append("{"); builder.Append("\"id\":" + new JavaScriptSerializer().Serialize(dt.Rows[i]["ClassifyID"].ToString())); builder.Append(","); builder.Append("\"text\":" + new JavaScriptSerializer().Serialize(dt.Rows[i]["CodeName"].ToString())); builder.Append(","); builder = InitChild(builder, dt.Rows[i]["ClassifyID"].ToString(), PClassiyList); builder.Append("}"); if (i < (dt.Rows.Count - 1)) { builder.Append(","); } } } builder.Append("]"); return builder; } public bool IsReusable { get { return false; } } } 效果图:
时间: 2024-09-30 23:27:45