Delphi TTreeNode recursively append child nodes to parent node

736 views Asked by At

I have an assignment in "project management". I have to assign modules which can also be sub-modules, so I want to append recursively sub-modules to modules.

Example:

P(project) Modules(M1,M2,M3,M4). Under M1 Module there will be sub-modules(M1S1,M1S2,M1S3), and under sub-module1 (M1S1) there can be many sub-modules (M1S1S1, M1S1S2, M1S1S3) and so on.

I have done this code using Recursion and TTreeNode but i feel the problem is with condition statement.

procedure TForm2.BitBtn1Click(Sender: TObject);

begin
 lGlblProjID := 1;
lGlblProjName := 'Project';
    ADOConnectionListner.Connected := true;
  try
    if ADOConnectionListner.Connected then
    begin

          RootNode := TreeView2.Items.Add(nil, lGlblProjName);
          getSubChild(lGlblProjID, RootNode);

   end;
    except
      on E: Exception do
      begin
        ShowMessage('Exception Class = ' + E.ClassName);
      end;
end;
end;

procedure TForm2.getSubChild(var Pid: Integer; var SubRoot: TTreeNode);
var
  lcount, I, lcurrentID: Integer;
  lcurrentName: String;
  lModuleNode: TTreeNode;

begin
  // ShowMessage(IntToStr(Pid)+ ' '+SubRoot.Text);

  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('SELECT * FROM treetab Where parent_id =:value1');
  ADOQuery1.Parameters.ParamByName('value1').Value := Pid;
  ADOQuery1.Active := true;
  lcount := ADOQuery1.RecordCount;

  for I := 0 to lcount - 1 do
  begin
    lcurrentID := ADOQuery1.FieldByName('id').AsInteger;
    lcurrentName := ADOQuery1.FieldByName('name').AsString;
    ShowMessage(' id ' + IntToStr(lcurrentID) + ' dd ' + lcurrentName);   // print valu of i

    if ((lcurrentID <> 0)and (SubRoot.Text <> '') ) then      //or
    begin
      lModuleNode := TreeView1.Items.AddChild(SubRoot, lcurrentName);
      getSubChild(lcurrentID, lModuleNode);
    end else // if
    // lcurrentID = 0

 ShowMessage('end reached');

 //  TreeView1.Items.AddChild(SubRoot, ADOQuery1.FieldByName('name').AsString);


 ADOQuery1.Next;
    //*********
  end;

end;

enter image description here

I want to retrieve all the sub-modules for a particular project like in this case project with id=1 only.

1

There are 1 answers

2
Tom Brunberg On BEST ANSWER

Your problem seems to be the non-local ADOQuery1 which gets cleared at entry on each recursive call. Therefore you loose all remaining records from a previous query. You should arrange a local storage for the query results.

Something like (untested):

procedure GetSubChild()
type
  TTempRecord = record
    id: integer;
    name: string;
  end;

  TTempArray = array of TTempRecord;
var
  lcount, I, lcurrentID: Integer;
  lcurrentName: String;
  lModuleNode: TTreeNode;
  recs: TTempArray
begin
  // ...
  // query the db
  // ...
  lcount := ADOQuery1.RecordCount;

  SetLength(recs, lcount);
  for i := 0 to lcount-1 do
  begin
    recs[i].id := ADOQuery1.FieldByName('id').AsInteger;
    recs[i].name := ADOQuery1.FieldByName('name').AsString;
    ADOQuery1.Next;
  end;

  for i := 0 to lcount-1 do
  begin
    lcurrentID := recs[i].id;
    lcurrentname := recs[i].name;
  // ...
  // add to treeview
  // call recursively GetSubChild()
  // ...
  end;
end;