2017-02-12 91 views
1

我在“项目管理”中有一项任务。我必须分配也可以是子模块的模块,所以我想递归地将子模块附加到模块。Delphi TTreeNode递归追加子节点到父节点

示例:

P(项目)模块(M1,M2,M3,M4)。 M1模块下面有子模块(M1S1,M1S2,M1S3),子模块1(M1S1)下面可以有多个子模块(M1S1S1,M1S1S2,M1S1S3)等等。

我已经使用递归和TTreeNode完成了这段代码,但是我感觉问题在于条件语句。

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

我想检索为特定项目的子模块像在这种情况下,项目与ID = 1只。

+0

什么“条件语句”你是说您有一个问题,你有什么关于它是错的关注? – MartynA

+0

下SSP考虑有1个更多的记录说S1 然后自去年那么叶SQL返回零和树建立半只 P1 -SP -SSP -S1 然后树休息。 – user3036212

回答

1

你的问题似乎是非本地ADOQuery1它在每次递归调用时都会被清除。因此,您将丢失先前查询中的所有剩余记录。您应该为查询结果安排本地存储。

喜欢的东西(未经测试):

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; 
+0

由于您提到的代码没有经过测试。我已经测试,它工作正常。谢谢。 – user3036212

+0

@user很高兴能帮助到您! –