前面的树形图,也就是TreeView一般读取的都是Json数据
例如
[{dispCode:01,dispName:'上级1, children: [
{dispCode:0101,dispname:'子级1' }, {dispCode:0102,dispname:'子级2' }, ] }]下面代码,读取的数据表里需要有以下字段
DispCode:编码 DispName:名称 level_num:级次
实现思路是先生成一个个对象(TeleItem),再将对象转化成JSONObject对象,再输出JSON文本。
可以调用REs.jSON里的TJson.ObjectToJsonString直接生成JSON文本,但是他会给输入增加一个Name叫XXXhelper。不是我要的。所以最终只能先自行将
TeleItem生成JSONObject再将jsonobjct生成Jsonobject。
Teleitem
实现代码
1 unit UntTree; 2 3 interface 4 uses System.Generics.Collections, FireDAC.Comp.Client,JSON,REST.Json,System.SysUtils; 5 type 6 TeleItem=class 7 dispcode:string; //编码 8 dispName:String; //名称 9 level_num:integer; //级次 10 children:TList<Teleitem>; //子级 11 destructor Destroy; override; 12 13 end; 14 15 16 function IntTree(FDQuery:TFDQuery;tableName,SetCode:String):String; //方法1 先生成Teleitem再生成Jsonobject 17 function LoadEleTree(FDQuery:TFDQuery;tableName,SetCode:String):String; //方法2 直接生成Jsonobject 18 19 implementation 20 function LoadEleTree(FDQuery:TFDQuery;tableName,SetCode:String):String; 21 var 22 FJOlist:TList<TJSONObject>; 23 FJData:TJSONObject; 24 I,levelnum,J:integer; 25 cDispCode:string; 26 JOParent,JO:TJsonObject; 27 JaData,Children:TJSONArray; 28 begin 29 FJData:=TJsonObject.Create; 30 FJOlist:=TList<TJSONObject>.Create; 31 JaData:=TJSONArray.Create; 32 FJData.AddPair('data',JaData); 33 FDQuery.Close; 34 FDQuery.open('select * from '+tableName +' where set_Code='''+setCode+''' and is_leaf<>10'); 35 FDQuery.FetchAll; 36 while Not FDQuery.Eof do 37 begin 38 try 39 JO:=TJSONObject.Create; 40 J:=FDQuery.FieldByName('level_num').AsInteger; 41 cDispCode:=FDQuery.FieldByName('disp_Code').asstring; 42 JO.AddPair('DispCode',cdispCode); 43 JO.AddPair('DispName',FDQuery.FieldByName('disp_Name').asstring); 44 JO.AddPair('LevelNum',INTTOSTR(J)); 45 46 FJOlist.Add(JO); 47 if FDQuery.FieldByName('level_num').asinteger<>1 then 48 begin 49 for I := 0 to FJOlist.Count-1 do 50 begin 51 JOParent:=FJOlist[I]; 52 levelnum:=JOParent.Values['LevelNum'].Value.tointeger; 53 if levelnum+1<>J then Continue; 54 var dispCode:=JOParent.Values['DispCode'].Value; 55 if Pos(dispcode,cdispCode)=1 then 56 begin 57 if not Assigned(JOParent.Values['Children']) then 58 begin 59 Children:=TJSONArray.Create; 60 JOParent.AddPair('Children',Children); 61 end else 62 Children:=JOParent.Values['Children'] as TJSONArray; 63 Children.Add(JO); 64 break; 65 end; 66 end; 67 end else 68 JaData.Add(JO); 69 finally 70 FDQuery.Next; 71 end; 72 end; 73 Result:=FJData.ToJSON(); 74 FJData.Free; 75 FJOlist.Free; 76 end; 77 function IntTree(FDQuery:TFDQuery;tableName,SetCode:String):String; 78 var 79 I:integer; 80 dispcode:String; 81 Data:TJSONObject; 82 FList,FTree:TList<TeleItem>; 83 {$REGION 'ConvertToObject'} 84 procedure ConvertToObject(Item:Teleitem;var Perent:TJSONArray); 85 var 86 aItem:TJSONObject; 87 I:Integer; 88 89 begin 90 aitem:=TJSONObject.Create; 91 aitem.AddPair('dispcode',item.dispcode); 92 aitem.AddPair('dispname',item.dispname); 93 Perent.add(aitem); 94 IF Not Assigned(Item.children) then Exit; 95 var P:=TJSONArray.Create; 96 aitem.AddPair('children',P); 97 For I:=0 to item.children.count-1 do 98 begin 99 ConvertToObject(item.children[I],P); 100 end; 101 102 end; 103 {$ENDREGION 'ConvertToObject'} 104 begin 105 FDQuery.Close; 106 FDQuery.open('select * from '+tableName +' where set_Code='''+setCode+''' and is_leaf<>10'); 107 FDQuery.FetchAll; 108 FList:=TList<Teleitem>.create; 109 FTree:=TList<Teleitem>.create; 110 while Not FDQuery.eof do 111 begin 112 try 113 Var AEle:=TEleitem.create; 114 aEle.dispCode:=FDQuery.FieldByName('disp_Code').asstring; 115 aele.dispName:=FDQuery.fieldByName('disp_Name').asstring; 116 AEle.level_num:=FDQuery.FieldByName('level_num').asinteger; 117 FList.Add(AEle); 118 if FDQuery.FieldByName('level_num').asinteger<>1 then 119 begin 120 for I := 0 to Flist.Count-1 do 121 begin 122 if Flist[I].level_num+1<>AEle.level_num then Continue; 123 dispCode:=Flist[I].dispCode; 124 if Pos(dispcode,aEle.dispCode)=1 then 125 begin 126 if not Assigned(Flist[I].children) then 127 Flist[I].children:=TList<Teleitem>.create; 128 FList[I].children.Add(aele); 129 break; 130 end; 131 132 end; 133 end else 134 FTree.Add(Aele); 135 finally 136 FDQuery.Next; 137 end; 138 end; 139 140 Data:=TJSONObject.create; 141 var P:=TJSONArray.Create; 142 data.AddPair('data',P); 143 for I := 0 to FTree.Count-1 do 144 begin 145 ConvertToObject(FTree[I],P); 146 end; 147 Result:=data.ToJSON(); 148 data.Free; 149 for I :=FList.count-1 downto 0 do 150 begin 151 Flist[I].Free; 152 end; 153 FList.Free; 154 FTree.Free; 155 end; 156 { TeleItem } 157 158 destructor TeleItem.Destroy; 159 begin 160 if Self.children<>nil then 161 Self.children.Free; 162 end; 163 164 end.
最终的结果如下,是一个json文本,然后前端如何生成Tree,自行研究