简单的关于树状视图的实现(50分)

  • 主题发起人 主题发起人 *呆客*
  • 开始时间 开始时间

*呆客*

Unregistered / Unconfirmed
GUEST, unregistred user!
朋友们,我向实现这样一个功能,我的数据库设计有三个字段,一个id,一个名称mc,一个父类id,我向通过这个数据库实现一个树状视图,我需要代码!
 
cxdbtreelist实现方式<br>设置datasource,设置keyfield,parentfield,ok<br><br>treeview或者类市控件无非是add,addchild
 
我就是想通过add,addchild实现,能不能给我说下这个方法如何实现的?
 
这个就是判断是否为子节点, 是的话然后addchild, 循环做一下就OK了
 
说的太笼统了吧
 
type<br>&nbsp; orgVO=record<br>&nbsp; &nbsp;PK_DEPART:string;<br>&nbsp; &nbsp;DEPCODE:string;<br>&nbsp; &nbsp;DEPNAME:string;<br>&nbsp; &nbsp;DEPTDESC:string;<br>&nbsp; &nbsp;DEPPHONE:string;<br>&nbsp; &nbsp;DEPADDR:string;<br>&nbsp; &nbsp;PARENTID:string;<br>&nbsp; &nbsp;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>&nbsp; for i:= 0 to sum-1 do<br>&nbsp; begin<br>&nbsp; &nbsp; if CompareStr(trim(vos.PARENTID),trim(vos.DEPCODE))=0 then begin<br>&nbsp; &nbsp; &nbsp; showmessage('请检查上下级编码是否一致,否则会造成死循环');<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; self.Close;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp; if (vos.PARENTID&lt;&gt;'') and (CompareStr(trim(vos.PARENTID),trim(str))=0) then begin<br>&nbsp; &nbsp; &nbsp; s:=vos.DEPCODE;<br>&nbsp; &nbsp; &nbsp; snode:=Treeview1.items.addchild(tnode,vos.DEPNAME);<br>&nbsp; &nbsp; &nbsp; dg(vos,snode,s,sum);<br>&nbsp; &nbsp; end;<br>&nbsp; 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>&nbsp; mysql:='select PK_DEPART,DEPCODE,DEPNAME,DEPTDESC,DEPPHONE,DEPADDR,PARENTID,LEVEL1 from BD_DEPART';<br>&nbsp; tt:=SQLzzorgDMO.Create.queryAll(mysql);<br>&nbsp; //组成全部数据的VO<br>&nbsp; j:=1;<br>&nbsp; setlength(vos,tt.DataSet.RecordCount);<br>&nbsp; while not tt.DataSet.Eof do begin<br>&nbsp; vo.PK_DEPART:=tt.DataSet.Fields.Fields[0].AsString;<br>&nbsp; vo.DEPCODE:=tt.DataSet.Fields.Fields[1].AsString;<br>&nbsp; vo.DEPNAME:=tt.DataSet.Fields.Fields[2].AsString;<br>&nbsp; vo.DEPTDESC:=tt.DataSet.Fields.Fields[3].AsString;<br>&nbsp; vo.DEPPHONE:=tt.DataSet.Fields.Fields[4].AsString;<br>&nbsp; vo.DEPADDR:=tt.DataSet.Fields.Fields[5].AsString;<br>&nbsp; vo.PARENTID:=tt.DataSet.Fields.Fields[6].AsString;<br>&nbsp; vo.LEVEL:=strtoint(tt.DataSet.Fields.Fields[7].AsString);<br>&nbsp; vos[j-1]:=vo;<br>&nbsp; inc(j);<br>&nbsp; tt.DataSet.Next;<br>&nbsp; end;<br>&nbsp; //写入数组<br>&nbsp; TreeView1.Items.Clear;<br>&nbsp; for j:=0 to high(vos) do begin<br>&nbsp; &nbsp; if trim(vos[j].PARENTID)='' then begin<br>&nbsp; &nbsp; &nbsp; RootNode:=TreeView1.Items.Add(nil,vos[j].DEPNAME);<br>//调用递归<br>&nbsp; &nbsp; &nbsp; dg(vos,RootNode,vos[j].DEPCODE,tt.DataSet.RecordCount);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
谢谢各位
 
后退
顶部