2017-02-12 1 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

는 S1 이후 그 다음 마지막 잎 SQL 리턴 전무와 나무가 내장되어 절반 만 P1 -SP -ssp -S1 다음 나무 휴식을 말한다 (1 개) 더 레코드가 SSP에서 고려한다. – 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 기꺼이 도와 드리겠습니다! –

관련 문제