When doing the system, there are some organizational structure of the relationship, into a tree-like structure, the front desk is Easyui, because it involves more than one table of data, want to dynamically splicing out a tree-like organization, the background is sent over a DataTable.
// <summary> /// DataTable to easyuitree JSON format // </summary> /// <param name= "dt" > table to convert </param> /// <param name= "Pfield" > parent node corresponding fields </param> /// <param name= "PValue" > parent node value, default to "0"</param> /// <param name= "IDfield" >ID corresponding field </param> /// <param name= "NameField" >name corresponding fields </param> /// <param name= "Isparentfield" > whether the parent node corresponds to a field </param> /// <param name= "Ischooseparentnode" > whether to select parent node, with single box, check box, True:checkbox radio can select parent node/ child nodes; False:radio can only select leaf nodes </param> /// <returns></returns> Public Static string Datatabletojsonelecviewtree(DataTable DT) {StringBuilder SB =NewStringBuilder ();stringFilter =string. Format ("{0}= ' {1} '","system_id",0);//Get top level directorydatarow[] DRS = dt. Select (filter);if(Drs.) Length <1) {return ""; } sb. Append ("[");//stitching display to front desk foreach(DataRow IteminchDRS) {sb. Append ("{"); Sb. AppendFormat ("\" id\ ": \" {0}\ ",", item["system_id"]. ToString ()); Sb. AppendFormat ("\" text\ ": \" {0}\ "", item["System_name"]. ToString ());//Get children node data stringFilterdoor =string. Format ("{0}= ' {1} '","system_id",2); datarow[] Drdoor = dt. Select (Filterdoor);stringSystemID = drdoor[0]["system_id"]. ToString (); DOORGUARDSYSTEMBLL DOORGUARDSYSTEMBLL =NewDOORGUARDSYSTEMBLL (); DataTable Dtdoor = Doorguardsystembll.getsystemdomain (int. Parse (SystemID));//stitching to parent node if(DtDoor.Rows.Count <1) {return ""; }Else{sb. Append (", \" children\ ": [");foreach(DataRow Itemsoninchdtdoor.rows) {sb. Append ("{"); Sb. AppendFormat ("\" id\ ": \" {0}\ ",", itemson["domain_id"]. ToString ()); Sb. AppendFormat ("\" text\ ": \" {0}\ "", itemson["domain_name"]. ToString ()); Sb. Append ("},"); }if(sb.) ToString (). EndsWith (",")) {sb. Remove (sb.) Length-1,1); } sb. Append ("]"); } sb. Append ("},"); }if(sb.) ToString (). EndsWith (",")) {sb. Remove (sb.) Length-1,1); } sb. Append ("]");returnSb. ToString (); }
When splicing tree, be sure to pay attention to some punctuation things, the wrong words he will not give hints, but also do not show the data, the test of careful place.
At the beginning of the spell, a little clue did not, in the online search of the bright a bit complex, do not understand, and then their own first-level down, although said now the cycle a bit more, and this is only two layers of the structure, but their own to find out the thing is a sense of accomplishment, and then optimize, first so use. Everyone can also try!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Beginners Learn jquery Easyui----DataTable Easyui Tree