Ext tree unlimited Dynamic tree

Source: Internet
Author: User
Code
Public void CreateData ()
{
/* Data Source
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> ();
}

// You only need to point the tree's dataurl to this page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.