using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq;public class Program { static void Main() { string conn = "server=xxx.xxx.xxx.xxx;database=xxxx;uid=xxx;pwd=xxx"; string cmd = "select * from xxxxx"; var da = new SqlDataAdapter(cmd, conn); var dt = new DataTable(); da.Fill(dt); Func<DataRow, Node> row2node = null; row2node = row => new Node { id = row.Field<string>("MENUID"), text = row.Field<string>("CAPTION"), children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r)).ToList() }; var nodes = dt.Select("[PARENT]='0'").Select(r => row2node(r)); string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(nodes); // 引用 System.Web.Extensions Console.WriteLine(json); } private class Node { public string id { get; set; } public string text { get; set; } public List<Node> children { get; set; } } }
不好意思是我弄错了,能帮我解释下这样写什么意思吗,谢谢row2node = row => new Node { id = row.Field<string>("MENUID"), text = row.Field<string>("CAPTION"), children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r)).ToList() };
这段等价于: private static Node row2node(DataRow row, DataTable dt) { return new Node { id = row.Field<string>("MENUID"), text = row.Field<string>("CAPTION"), children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r, dt)).ToList() }; }这就是个自己调用自己的递归方法。
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link href="http://wwwendt.de/tech/dynatree/src/skin/ui.dynatree.css" rel="stylesheet" type="text/css">
<script src="http://wwwendt.de/tech/dynatree/src/jquery.dynatree.js" type="text/javascript"></script>
<div id="tree"> </div> <script>
var data =
[
{"id": 12, "text": "节点1", "children":
[
{ "id": 11, "text": "节点1.1" }
]
},
{"id": 12, "text": "节点1", "children":
[
{"id": 11, "text": "节点1.1", "children":
[
{ "id": 11, "text": "节点1.1" }
]
}
]
}
]; function processNodes(nodes) {
for (var i=0; i<nodes.length; i++) {
nodes[i].title = nodes[i].text;
if (nodes[i].children)
processNodes(nodes[i].children);
}
return nodes;
}
$(function () {
$("#tree").dynatree({
children: processNodes(data)
});
});
</script>
{ "id":12,"text": "节点1", "children":
[
{ "id":11,"text": "节点1.1" }
]
},
{ "id":12,"text": "节点1", "children":
[
{ "id":11,"text": "节点1.1" ,"children":
[
{ "id":11,"text": "节点1.1" }
]
}
]
}
]id跟text的值都是从数据库里查询出来,在用程序把json拼接,就可以显示出下拉树形
ds.Tables[0].Rows[i]["parent"]这是id值
ds.Tables[0].Rows[i]["text"] text的值
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;public class Program
{
static void Main()
{
string conn = "server=xxx.xxx.xxx.xxx;database=xxxx;uid=xxx;pwd=xxx";
string cmd = "select * from xxxxx";
var da = new SqlDataAdapter(cmd, conn);
var dt = new DataTable();
da.Fill(dt); Func<DataRow, Node> row2node = null;
row2node = row => new Node
{
id = row.Field<string>("MENUID"),
text = row.Field<string>("CAPTION"),
children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r)).ToList()
}; var nodes = dt.Select("[PARENT]='0'").Select(r => row2node(r));
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(nodes); // 引用 System.Web.Extensions
Console.WriteLine(json);
} private class Node
{
public string id { get; set; }
public string text { get; set; }
public List<Node> children { get; set; }
}
}
{
id = row.Field<string>("MENUID"),
text = row.Field<string>("CAPTION"),
children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r)).ToList()
};
{
return new Node
{
id = row.Field<string>("MENUID"),
text = row.Field<string>("CAPTION"),
children = dt.Select("[PARENT]='" + row.Field<string>("MENUID") + "'").Select(r => row2node(r, dt)).ToList()
};
}这就是个自己调用自己的递归方法。