![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public void CreateData()
{
/*数据源
Common.RunParameter.Instance().DataLink = @"Data Source=SP002\MSSQLSERVERS; Initial Catalog=SP2008_ZC_01; uid=sa; pwd=***; Pooling=True;";
Common.RunParameter.Instance().DataType = "SqlServer";
DataTable dt = Database.DBOperate.ExecuteDataset("Select tGuid,tCode,tName,tRoot,tRank From ScTeam where tDele = 0 Order By tCode").Tables[0];
//Common.RunParameter.Instance().DataLink = @"Data Source=SP002\MSSQLSERVERS; Initial Catalog=SP2008_JX_01; uid=sa; pwd=***; Pooling=True;";
//Common.RunParameter.Instance().DataType = "SqlServer";
//DataTable dt = Database.DBOperate.ExecuteDataset("Select rGuid,rCode,rName,rRoot,rRank From ScRace where rDele = 0 Order By rCode").Tables[0];
*/
JavaScriptSerializer serializer = new JavaScriptSerializer();
string result = serializer.Serialize(getJson(dt));
Response.Write(result);
}
private static Nodes[] getJson(DataTable dt)
{
var node = new List<Nodes>();
foreach (DataRow row in dt.Rows)
{
if (int.Parse(row["tRank"].ToString()) != 1) continue;
var code = row[1].ToString();
var r = dt.Select(string.Format("{0}='{1}'", dt.Columns[3].ColumnName, code), "");
Nodes n;
if (r.Length == 0)
{
n = new Nodes { id = row[0].ToString(), text = row[2].ToString(), leaf = true };
}
else
{
n = new Nodes { id = row[0].ToString(), text = row[2].ToString() };
n = getNodes(code, dt, n);
}
node.Add(n);
}
return node.ToArray();
}
private static Nodes getNodes(string code, DataTable dt, Nodes n)
{
var nc = n;
foreach (var row in dt.Select(string.Format("{0}='{1}'", dt.Columns[3].ColumnName, code), ""))
{
var c = row[1].ToString();
var r = dt.Select(string.Format("{0}='{1}'", dt.Columns[3].ColumnName, c), "");
Nodes ns;
if (r.Length == 0)
{
ns = new Nodes { id = row[0].ToString(), text = row[2].ToString(), leaf = true };
}
else
{
ns = new Nodes { id = row[0].ToString(), text = row[2].ToString() };
getNodes(c, dt, ns);
}
nc.children.Add(ns);
}
return nc;
}
}
class Nodes
{
public string id;
public string text;
public bool leaf;
public List<Nodes> children = new List<Nodes>();
}
//只要将树的dataurl指像这个页面即可