Jump to content

Help me UniTreeview+UniDbgrid+Mysql


Ulugbek

Recommended Posts

Hi All

Who can help me

How work or how fill treeview from database..

I need this for items table

structure

Items

CREATE TABLE [goods] (
  [id] integer NOT NULL PRIMARY KEY AUTOINCREMENT, 
  [description] varchar(255), 
  [parentid] integer, 
  [isgroup] integer, 
  [code] VARCHAR(25),   
  [note] VARCHAR(2048), 

All Items
    TV
       LCD    
       LED
    VIDEO


Link to comment
Share on other sites

procedure TMainForm.LoadMenu;
  var
             nod1:TUniTreeNode;
 
  procedure AddSons(xnod:TUniTreeNode; pid:integer);
    var
            q:TUniQuery;                         // or   q:TZQuery, TAdoQuery or the DB engine you work with
         nod2:TUniTreeNode;
    begin
    q:=TUniQuery.Create(Self);
    try
       q.Connection:=UniMainModule.Conn;
       q.Close;
       q.SQL.Clear;
       q.SQL.Add('select * from goods');
       q.SQL.Add('where parent_id=:pid');
       q.SQL.Add('order by id');
       q.ParamByName('pid').Value   :=pid;
       q.Open;
       while not q.eof do
             begin
             nod2:=TreeView.Items.AddChild(xNod, q.FieldByName(' whatever field holds the text of the line ').AsString);
             Nod2.ImageIndex:=0;  //Or the id of the picture from the ImageList
             AddSons(Nod2, q.FieldByName('id').AsInteger);                                //Next level ! Recursiv !!
             q.Next;
             end;
       q.Close;
    finally
       q.Free;
    end;
    end;
 
  begin
  with TreeView do
       begin
       Items.Clear;
       Sel.Close;                                 //Sel is a TUniQuery that is allready on the form
       Sel.SQL.Clear;
       Sel.SQL.Add('select * from goods where parent_id=0 order by id');  //The linked list starts from parent_id=0
       Sel.Open;
       while not Sel.EOF do
             begin
             nod1:=Items.Add(nil, Sel.FieldByName(' field you what you want to display in the line..... ').AsString);
             Nod1.ImageIndex:=0;
             AddSons(Nod1, Sel.FieldByName('id').AsInteger);
             Sel.Next;
             end;
       Sel.Close;
       end;
  end;
 
N.B.

Replace the fields with the name of the ones you want.

The list can go indefinitely deep.

Check that you do not generate an eternal loop.

 

Have fun !!

  • Thanks 1
Link to comment
Share on other sites

You edit, add, delete from the goods table.

 

Ex:

 

id            parent_id     description

==============================

1                0                TV

2                0                VIDEO

3                1                LCD

4                1                LED

5                2                SAMSUNG

6                2                SONY

..... and so on 

Link to comment
Share on other sites

I want to add by TreeView but how.. I want new group and I stay in Treeview and add node or childnode after then refresh node or reopen or itemsearch can stay new node some like But How?

Link to comment
Share on other sites

  • 2 weeks later...

Help me how add node or child nod in runtime?

 

 

Hi Ulugbek.

 

If I understand correctly, then you can:
 
UniTreeView1.Items.AddChild (nil, 'Test node');

UniTreeView1.Items.AddChild (UniTreeView1.Selected, 'Test child node');

 

To save the data in the database can be something like this:
 
I'm using Firebird ...
I can advise the following:
 
At the beginning of the addition of nodes to use in the Data property node (
AUniTreeNode.Data: = TObject (ID);
 ) ;
 
 
If you add a child node, you can use the following code:
 
1. It is necessary to generate the ID in the code, not in the database, you can use FireBird:
     SELECT GEN_ID (<GeneratorName>, <increment>) FROM DB $ DATABASE; (further be a parent node to the other nodes);
 
2. You can add in the beginning of the database, and then in UniTree ......
 
//sample code

var ID: Integer;
  ANode: TUniTreeNode;
  AChildNode: TUniTreeNode;

begin
  
.......

  ANode: = UniTreeView1.Selected;

  AChildNode := UniTreeView1.Items.AddChild (ANode, 'test child node');
  AChildNode.Data: = TObject (your generated ID) ;


  // Insert into DataBase
  //insert into yourTable (ID, PARENT_ID, DESCRIPTION .....)
  //values ​​(:ID, :PARENT_ID, :DESCRIPTION .....)
  //yourDataSet.ParamByName ('ID').asInteger: = your generated ID;
  //yourDataSet.ParamByName ('PARENT_ID').asInteger: = Integer (ANode.Data);
  //yourDataSet.ParamByName ('DESCRIPTION').asString := 'test child node';

...........

end;

 

Sincerely

Link to comment
Share on other sites

  • 1 month later...
  • 6 years later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...