type<br> orgVO=record<br> PK_DEPART:string;<br> DEPCODE:string;<br> DEPNAME:string;<br> DEPTDESC:string;<br> DEPPHONE:string;<br> DEPADDR:string;<br> PARENTID:string;<br> LEVEL:integer;<br>end;<br><br><br>递归函数:<br>procedure Tdlg_org.dg(vos: array of zzorgVO;tnode:TTreeNode;str:string;sum:integer);<br>var<br>i:integer;<br>s:string;<br>snode:TTreeNode;<br>begin<br> for i:= 0 to sum-1 do<br> begin<br> if CompareStr(trim(vos.PARENTID),trim(vos.DEPCODE))=0 then begin<br> showmessage('请检查上下级编码是否一致,否则会造成死循环');<br> exit;<br> self.Close;<br> end;<br> if (vos.PARENTID<>'') and (CompareStr(trim(vos.PARENTID),trim(str))=0) then begin<br> s:=vos.DEPCODE;<br> snode:=Treeview1.items.addchild(tnode,vos.DEPNAME);<br> dg(vos,snode,s,sum);<br> end;<br> end;<br>end;<br><br>调用递归的方法:<br>procedure Tdlg_org.frushTree;<br>var<br>mysql:string;<br>tt:TDataSource;<br>j:integer;<br>vos:array of zzorgVO;<br>vo:zzorgVO;<br>RootNode:TTreeNode;<br>begin<br> mysql:='select PK_DEPART,DEPCODE,DEPNAME,DEPTDESC,DEPPHONE,DEPADDR,PARENTID,LEVEL1 from BD_DEPART';<br> tt:=SQLzzorgDMO.Create.queryAll(mysql);<br> //组成全部数据的VO<br> j:=1;<br> setlength(vos,tt.DataSet.RecordCount);<br> while not tt.DataSet.Eof do begin<br> vo.PK_DEPART:=tt.DataSet.Fields.Fields[0].AsString;<br> vo.DEPCODE:=tt.DataSet.Fields.Fields[1].AsString;<br> vo.DEPNAME:=tt.DataSet.Fields.Fields[2].AsString;<br> vo.DEPTDESC:=tt.DataSet.Fields.Fields[3].AsString;<br> vo.DEPPHONE:=tt.DataSet.Fields.Fields[4].AsString;<br> vo.DEPADDR:=tt.DataSet.Fields.Fields[5].AsString;<br> vo.PARENTID:=tt.DataSet.Fields.Fields[6].AsString;<br> vo.LEVEL:=strtoint(tt.DataSet.Fields.Fields[7].AsString);<br> vos[j-1]:=vo;<br> inc(j);<br> tt.DataSet.Next;<br> end;<br> //写入数组<br> TreeView1.Items.Clear;<br> for j:=0 to high(vos) do begin<br> if trim(vos[j].PARENTID)='' then begin<br> RootNode:=TreeView1.Items.Add(nil,vos[j].DEPNAME);<br>//调用递归<br> dg(vos,RootNode,vos[j].DEPCODE,tt.DataSet.RecordCount);<br> end;<br> end;<br>end;