加载树形结构

来源:互联网 发布:休闲零食 大数据 编辑:程序博客网 时间:2024/06/05 05:58

表SoftClass:
SID   编号 自动编号
ATEXT 文字 字符
PID 父节点 数字

type //软件分类结构
 PSoftClass=^TSoftClass;
 TSoftClass=record
  SID:String; //软件分类编号
  SoftText:String;//分类文字
  PID:String;//父编号
 end;

//查询,返回成功或失败
function GetRecordSet(var adoQry:TADOQuery;strSQL:String):Boolean;
//加载软件分类
procedure LoadSoftClass(adoQry:TADOQuery;tv:TTreeView;var PSoft:array of PSoftClass);
//根据父节点加载
function LoadNodeFromParent(tv:TTreeView;var PSoft:array of PSoftClass;CurP:Integer;PID:String;ParentNode:TTreeNode;cdsTree:TClientDataset):Boolean;

//根据父节点加载
function LoadNodeFromParent(tv:TTreeView;var PSoft:array of PSoftClass;CurP:Integer;PID:String;ParentNode:TTreeNode;cdsTree:TClientDataset):Boolean;
var
 ret:Boolean;
 NewNode:TTreeNode;
 a:PString;
 cdsTmp:TClientDataset;
begin
  try
   cdsTmp:=TClientDataset.Create(nil);
   cdsTmp.Data:=cdsTree.Data;
   with cdsTmp do
   begin
     Filtered:=False;
     Filter:='PID='+PID;
     Filtered:=True;
     //First;
     while not eof do
     begin
       new(PSoft[CurP]);
       PSoft[CurP].SID:=FieldByName('SID').AsString;
       PSoft[CurP].SoftText:=FieldByName('AText').AsString;
       PSoft[CurP].PID:=FieldByName('PID').AsString;
       NewNode:=tv.Items.AddChildObject(ParentNode,PSoft[CurP].SoftText,TObject(PSoft[CurP]));
       if not (ParentNode=nil) then ParentNode.Expand(True);
       Inc(CurP);
       ret:=LoadNodeFromParent(tv,Psoft,CurP,FieldByName('SID').AsString,NewNode,cdsTmp);
       next;
     end;//while
     Filtered:=False;
   end;//with
  finally
   cdsTmp.Free;
  end;//try
  result:=ret;
end;


//加载软件分类
procedure LoadSoftClass(adoQry:TADOQuery;tv:TTreeView;var PSoft:array of PSoftClass);
var
  I:Integer;
  cdsTmp:TClientDataset;
begin
  tv.Items.Clear;
  try
   cdsTmp:=TClientDataset.Create(nil);
   with cdsTmp.FieldDefs do
   begin
    add('SID', ftInteger); //序号
    add('AText', ftString, 50, false); //记录值
    add('PID', ftInteger); //序号
   end;//with
  cdsTmp.CreateDataSet;
  with adoQry do
  begin
   if not IsEmpty then
   begin
    First;
    while not eof do
    begin
     cdsTmp.Append;
     cdsTmp.FieldByName('SID').AsInteger:=Fieldbyname('SID').AsInteger;
     cdsTmp.FieldByName('ATEXT').AsString:=FieldByName('ATEXT').AsString;
     cdsTmp.FieldByName('PID').AsInteger:=Fieldbyname('PID').AsInteger;
     cdsTmp.Post;
     Next;
    end;//while
   end;//if
  end;//with
  //加载根节点
   I:=0;
   LoadNodeFromParent(tv,PSoft,I,'-1',nil,cdsTmp);
  finally
   cdsTmp.Free;
  end;
end;


function GetRecordSet(var adoQry:TADOQuery;strSQL:String):Boolean;
begin
  Result:=False;
  try
   with adoQry do
   begin
    Connection:=DataM.adoConn;
    Close;
    SQL.Clear;
    SQL.Add(strSQL);
    //WriteLog(strSQL);
    Open;
   end;
   if not adoQry.IsEmpty then
     Result:=True;
  except
   //Result:=False;
  end;
end;

调用:
var
 PSoft:array of PSoftClass;
 adoQry:TADOQuery;

   adoQry:=TADOQuery.Create(nil);
   sSQL:='select * from SoftClass';
   if not GetRecordSet(adoQry,sSQL) then
    exit;
SetLength(PSoft,adoQry.RecordCount);
LoadSoftClass(adoQry,t,PSoft);