Solved

Generate a treeview

Posted on 1999-01-11
4
172 Views
Last Modified: 2010-04-06
I have a dataset with fields 'son and 'father'.
(root with father=spaces)(likely subdirectory directory)
Do you know any algorithm generate a tree with
this dataset?
Please tell me. Thanks.
0
Comment
Question by:mathematics
  • 2
4 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1355996
There are some data aware tree views around. Take a look on DSP(http://sunsite.icm.edu.pl/delphi) - there is a search engine.

Cheers,

Raymond.
0
 
LVL 1

Accepted Solution

by:
apaparis earned 50 total points
ID: 1355997
If Id is primary key
and father is pointing to father record

// Body
DataSet = "Select * from table where father = 0 order by Id"
while not eof begin
   appendSubtree(Id)
   DataSet.Next
end

Procedure appendSubtree(AnId)
   currentTreeNode := <-- append current rec to tree root
   SubDataSet := "Select * from Table Where Father = " + IntToStr(AnId)

   While Not SubDataSet.Eof Begin
      AppendSubTree (Id)
      SubDataSet.Next
   End
End

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1355998
hi mathematics,

instead teh table i used, you can do it with a dataset also,

procedure TForm1.Button1Click(Sender: TObject);
var
  Found : Boolean;
  I : Integer;
  t : TTreeNode;
begin
  { Clear Treeview }
  while treeview1.Items.count > 0 do treeview1.Items[0].delete;
  { Create a root }
  treeview1.Items.Add(treeview1.TopItem,Table1.TableName);
  table1.open;
  table1.first;
  while not(Table1.eof) do
  begin
    i := 0;
    Found := False;

    { Search for Father }

    while Not(Found) and (i < TreeView1.Items.Count) do
    begin
      Found := Table1.FieldByName('Father').AsString = Treeview1.Items[i].Text;
      if Not(Found) then inc(i);
    end;

    If Found then  {Father exists, add son }
    begin
      t := TreeView1.Items.AddChild(TreeView1.Items[i],Table1.FieldByName('Son').AsString);
       { Do Something else with t }
    end
    else
    begin     {Father not exists, add Father to root, add son }
      t := TreeView1.Items.AddChild(TreeView1.TopItem,Table1.FieldByName('Father').AsString);
       { Do Something else with t }
      t := TreeView1.Items.AddChild(t,Table1.FieldByName('Son').AsString);
       { Do Something else with t }
    end;
    Table1.Next; {Next Record}
  end;
  table1.Close;
end;

can be optimized, of course

meikl
0
 
LVL 1

Expert Comment

by:apaparis
ID: 1355999
Sorry. This is right

If Id is primary key
and father is pointing to father record

// Body
DataSet = "Select * from table where father = 0 order by Id"
while not eof begin
   currentNode := <-- append current rec to tree root
   appendSubtree(Id, CurrentNode)
   DataSet.Next
end

Procedure appendSubtree(AnId, ANode)
   SubDataSet := "Select * from Table Where Father = " + IntToStr(AnId)

   While Not SubDataSet.Eof Begin
      SubTreeNode := <-- append current rec under ANode          
      AppendSubTree (Id, SubTreeNode)  
      SubDataSet.Next
   End
End

 
 
 
   
 

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now