绑定树形菜单

jiang_chao 2013-06-17 02:02:02
请问一下,不用asp.net的控件treeview,用jquery的某一种树形,怎么遍历某个文件绑成这样子,谢谢!
...全文
705 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kingjt 2015-04-03
后台代码是放在getTree.ashx里面的,就是建一个“一般处理程序”
回复
kingjt 2015-04-03
后台代码: using System; using System.Web; using System.Configuration; using System.Data; using System.Text; using System.Collections.Generic; //add using System.Web.Script.Serialization; namespace Backstage { /// <summary> /// getMenuByRuleId 的摘要说明 /// </summary> public class getTree : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string ruleId = context.Request.QueryString["pId"].ToString().Trim(); DataTable dt = createDT(); if (context.Request.QueryString["pId"] != null && context.Request.QueryString["pId"].ToString().Trim() != "" && context.Request.Params["id"] == null) { context.Response.Write("[" + createJsonDataList(context.Request.QueryString["pId"], dt) + "]"); } else if (context.Request.Params["id"] != null && context.Request.Params["id"].ToString().Trim() != "") { context.Response.Write("[" + createJsonDataList(context.Request.Params["id"].ToString().Trim(), dt) + "]"); } context.Response.End(); } protected static DataTable createDT() { DataTable dt = new DataTable(); dt.Columns.Add("module_id"); dt.Columns.Add("module_name"); dt.Columns.Add("module_fatherid"); dt.Columns.Add("module_url"); dt.Columns.Add("module_order"); dt.Columns.Add("NODETYPE"); dt.Rows.Add("C1", "系统管理", "0", "1.aspx", "1", "0"); dt.Rows.Add("M01", "后台管理", "C1", "2.aspx", "1", "0"); dt.Rows.Add("M0101", "用户管理", "M01", "3.aspx", "100", "0"); dt.Rows.Add("M010101", "组织机构", "M0101", "4.aspx", "1000", "1"); dt.Rows.Add("M010102", "用户列表", "M0101", "", "1001", "1"); //dt.Rows.Add("M010103", "福田区", "M0101", "", "1002", "1"); //dt.Rows.Add("M010104", "宝安区", "M0101", "", "1003", "1"); //dt.Rows.Add("M010105", "龙岗区", "M0101", "", "1004", "1"); dt.Rows.Add("M010103", "权限管理", "M01", "", "1002001", "0"); dt.Rows.Add("M01010302", "菜单管理", "M010103", "", "1002002", "1"); dt.Rows.Add("M01010303", "角色管理", "M010103", "", "1002003", "1"); dt.Rows.Add("M01010304", "功能权限", "M010103", "", "1002004", "1"); dt.Rows.Add("M01010305", "用户角色", "M010103", "", "1002005", "1"); //dt.Rows.Add("M01010306", "华强北", "M010103", "", "1002006", "1"); dt.Rows.Add("M0102", "资产管理", "M01", "", "101", "0"); dt.Rows.Add("M010201", "资产列表", "M0102", "", "1105", "1"); dt.Rows.Add("M010202", "参数管理", "M0102", "", "1106", "1"); //dt.Rows.Add("M010203", "天河区", "M0102", "", "1107", "1"); //dt.Rows.Add("M010204", "白云区", "M0102", "", "1108", "1"); //dt.Rows.Add("M010205", "黄埔区", "M0102", "", "1109", "1"); //dt.Rows.Add("M010206", "荔湾区", "M0102", "", "1110", "1"); //dt.Rows.Add("M010207", "罗岗区", "M0102", "", "1111", "1"); //dt.Rows.Add("M010208", "南沙区", "M0102", "", "1112", "1"); return dt; } /// <summary> /// /// </summary> /// <param name="strPid"></param> /// <returns></returns> // NODETYPE==-1开始 NODETYPE==0文件夹 NODETYPE==3文件 传入pid private string createJsonDataList(string strPid, DataTable dtAll) { DataRow[] drs = dtAll.Select(" module_fatherid='" + strPid + "' "); StringBuilder sb = new StringBuilder(""); for (int i = 0; i < drs.Length; ++i) { string strNodeType = drs[i]["NODETYPE"].ToString().Trim(); string strNodeId = drs[i]["module_id"].ToString().Trim(); string strNodeText = drs[i]["module_name"].ToString().Trim(); bool hasChild = false; if (strNodeType == "-1" || strNodeType == "0") { hasChild = true; } sb.Append(createJsonData(strNodeId, strNodeText, hasChild)); sb.Append(","); } return sb.ToString().Trim().Substring(0, sb.ToString().Trim().Length - 1); } private string createJsonData(string strId, string strText, bool hasChild) { StringBuilder sb = new StringBuilder(""); sb.Append("{\"id\":\"" + strId + "\",\"text\":\"" + strText + "\","); if (hasChild) { sb.Append("\"state\":\"closed\",\"attributes\":{\"NodeType\":\"1\"}"); } else { sb.Append("\"state\":\"open\",\"attributes\":{\"NodeType\":\"0\"}"); } sb.Append("}"); return sb.ToString().Trim(); } public bool IsReusable { get { return false; } } } } 前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeTest.aspx.cs" Inherits="Backstage.TreeTest" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link rel="stylesheet" type="text/css" href="js/easyui/themes/default/easyui.css" /> <link rel="stylesheet" type="text/css" href="js/easyui/themes/default/tree.css" /> <link rel="stylesheet" type="text/css" href="js/easyui/themes/icon.css" /> <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="js/easyui/jquery.easyui.min.js"></script> <script type="text/javascript"> $(function () { $('#MyTree').tree({ checkbox: true, url: 'getTree.ashx?pId=0', onClick: function (node) { var state = node.state; alert(node.id); if (node.attributes.NodeType == 1) { //判断当前选中的节点是否为根节点 // alert("有子节点"); } else { // alert("无子节点"); } }, animate: true, onBeforeExpand: function (node, param) { $('#MyTree').tree('options').url = "getTree.ashx?pId=" + node.id; } }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <div id="categoryChooseDiv" title="请选择分类" style="width: 650px; height: 300px;"> <ul id="MyTree"></ul> </div> </div> </form> </body> </html>
回复
kingjt 2015-04-03
在后台构造JSON格式数据,传到客户端,而 不是在服务器段代码绑定
回复
NENGAH 2013-07-11
获取根 节点 根节点 一般就有个 属性 子节点属性一类的(好好找找)如Nodes 这个属性下 一般都会有神马Add一类的 把要加的属性 往上面放就对了 树形 遍历 一般都是用递归...
回复
jiang_chao 2013-06-17
引用 3 楼 Chinajiyong 的回复:
[quote=引用 2 楼 jiang_chao 的回复:] 我知道很多插件,可我就是绑定不了,我刚才发的图片那个样子, DirectoryInfo thisOne = new DirectoryInfo(Path); DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录 foreach (DirectoryInfo dirinfo in subDirectories) { } 递归,不知道怎么绑定,版主能指点一下吗?
像jsTree一般可以支持XML,JSON,Html三种数据源。你可以获得目录转化为json格式,再绑定啥 看看这个http://www.jb51.net/article/24375.htm [/quote] 我这样遍历出来了,就是不知道怎么绑定到jquery.treeview或者jstree上面, public string ListTreeShow(DirectoryInfo theDir)//递归目录 文件 { DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录 string resultStr = string.Empty; foreach (DirectoryInfo dirinfo in subDirectories) { } ListTreeShow(dirinfo) }
回复
mmqw 2013-06-17
去弄一个dtree的控件就可以
回复
可以采用jeasyui的tree www.jeasyui.com 里面有source
回复
EnForGrass 2013-06-17
引用 2 楼 jiang_chao 的回复:
我知道很多插件,可我就是绑定不了,我刚才发的图片那个样子, DirectoryInfo thisOne = new DirectoryInfo(Path); DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录 foreach (DirectoryInfo dirinfo in subDirectories) { } 递归,不知道怎么绑定,版主能指点一下吗?
像jsTree一般可以支持XML,JSON,Html三种数据源。你可以获得目录转化为json格式,再绑定啥 看看这个http://www.jb51.net/article/24375.htm
回复
jiang_chao 2013-06-17
我知道很多插件,可我就是绑定不了,我刚才发的图片那个样子, DirectoryInfo thisOne = new DirectoryInfo(Path); DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录 foreach (DirectoryInfo dirinfo in subDirectories) { } 递归,不知道怎么绑定,版主能指点一下吗?
回复
EnForGrass 2013-06-17
回复
相关推荐
发帖
.NET技术前瞻
创建于2007-09-28

1.3w+

社区成员

.NET技术 .NET技术前瞻
申请成为版主
帖子事件
创建了帖子
2013-06-17 02:02
社区公告
暂无公告