TTreeView and TClientDataSet Sample


Mas Rahman, menanyakan tentang penggunaan TreeView dengan database. Aku sendiri menganjurkan TreeView dari DevExpress karena sangat mudah dan intuitif. Jedi VCL juga memiliki JDbTreeView tetapi penggunaannya sangat tidak intuitif dan manualnya sulit didapatkan. Searching di google, di milis Jedi saja ada komentar berikut:

I had successfully implemented DevExpress’s DBTreeview when I worked for
my last employer, so I thought I could apply what I had learned in my
home project using JvDBTreeview, but failed after many hours.
Apparently, they are similar, but not similar enough. I also searched
the help files and the internet extensively for any info about this
component and finally gave up looking and bought DevExpress’s DBTreeview
Suite. With the aid of their very well written help files and online
support files, I was able to create exactly what I wanted in about two
hours.

Warning: Woll2Woll’s very nice DB treeview is easier to implement than
DevExpress’s, but shields the programmer from learning how the treeview
really works and actually creates work for the programmer when they try
to override its default behavior.
I recommend DevExpress’s treeview over W2W’s. If yo

u buy it, I’ll help
you get it running.

Good luck.
Regards,
Sam Hunt

Jadi menurut saya kalau memang tidak beli TreeView dari DevExpress ya mau tidak mau menggunakan TTreeView bawaa Delphi. Nah butuh rekursif prosedur untuk bisa membaca seluruhnya.

Nah berikut ini adalah coding sample penggunaan untuk loading tree structure dari database. Intinya, setiap row butuh 1 parent ID dan ID dia sendiri, termasuk field untuk menampilkan data di treenya. Jadi minimal butuh 3 field. Parent ID, Child ID, dan Caption yang akan dimunculkan.

Untuk membuatnya, drop komponen berikut ke form:

  1. TTreeView
  2. Button dengan caption Refresh Tree
  3. TClientDataSet (atau gunakan komponen TTable atau TQuery atau turunan TDataSet)
  4. TDataSource
  5. TDBGrid
  6. TDBNavigator
 
unit unitMain;

interface

uses
  Windows, Messages, SysUtils, Variants,
  Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  DBCtrls, Grids, DBGrids,  ComCtrls, DB,
  DBClient;

type
  TformMain = class(TForm)
    treeView: TTreeView;
    tbmSample: TClientDataSet;
    tbmSampleParentID: TStringField;
    tbmSampleChildID: TStringField;
    tbmSampleCaption: TStringField;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    dtsSample: TDataSource;
    btnRefresh: TButton;
    procedure btnRefreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure CreateTree(parentID: String; ParentNode: TTreeNode);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  formMain: TformMain;

implementation

{$R *.dfm}

procedure TformMain.CreateTree(parentID: String; ParentNode: TTreeNode);
var
  list: TStringList;
  i: integer;
  newNode: TTreeNode;
begin
  list := TStringList.Create;

  tbmSample.Filtered := False;
  tbmSample.Filter := 'ParentID = ' + QuotedStr(parentID);
  tbmSample.Filtered := True;

  while Not tbmSample.EOF do
  begin
    if tbmSample.FieldByName('ParentID').AsString = parentID then
      list.Add(tbmSample.FieldByName('ChildID').AsString + '=' +
        tbmSample.FieldByName('Caption').AsString);
    tbmSample.Next;
  end;

  i := 0;
  while i < list.Count do
  begin
    begin
      newNode := treeView.Items.AddChild(ParentNode,
        list.Values[list.Names[i]]);

      CreateTree(list.Names[i], newNode);

      Inc(i);

      if i Mod 10 = 0 then
        Application.ProcessMessages;
    end;
  end;

  list.Free;
end;

procedure TformMain.FormCreate(Sender: TObject);
begin
  tbmSample.Open;
  tbmSample.Insert;
  tbmSampleParentID.AsString := '0';
  tbmSampleChildID.AsString := '1';
  tbmSampleCaption.AsString := 'Wisnu';
  tbmSample.Post;

  tbmSample.Insert;
  tbmSampleParentID.AsString := '1';
  tbmSampleChildID.AsString := '2';
  tbmSampleCaption.AsString := 'Rayyan';
  tbmSample.Post;

  tbmSample.Insert;
  tbmSampleParentID.AsString := '1';
  tbmSampleChildID.AsString := '3';
  tbmSampleCaption.AsString := 'Aila';
  tbmSample.Post;

  tbmSample.Insert;
  tbmSampleParentID.AsString := '0';
  tbmSampleChildID.AsString := '4';
  tbmSampleCaption.AsString := 'Rachman';
  tbmSample.Post;

  tbmSample.Insert;
  tbmSampleParentID.AsString := '4';
  tbmSampleChildID.AsString := '5';
  tbmSampleCaption.AsString := 'Fina';
  tbmSample.Post;

  tbmSample.Insert;
  tbmSampleParentID.AsString := '4';
  tbmSampleChildID.AsString := '6';
  tbmSampleCaption.AsString := 'Fani';
  tbmSample.Post;
end;

procedure TformMain.btnRefreshClick(Sender: TObject);
var
  topNode: TTreeNode;
begin
  topNode := treeView.Items.Add(Nil, 'Root Node');
  CreateTree('0', topNode);
  tbmSample.Filtered := False;
end;

end.
 

Untuk project lengkapnya, download saja di sini.

Tampilan Aplikasi

Tampilan Aplikasi

Iklan

Posted on Oktober 24, 2011, in Pemrograman and tagged , , . Bookmark the permalink. 6 Komentar.

  1. OK tak coba terapkan dalam Form Chart of Accounts..
    dengan treeview ini user tidak akan salah memilih account header..

    Makasih

  2. Kasih tahu kalau udah bisa ya Mas..

  3. Mas wisnu kalo mau menampilkan form dari menu treeview dengan memanggil data pada field caption contoh kodingnya seperti apa???

    • Maksud Mas Wahyudi kalau di klik di caption TreeView mau menampilkan form terkait dengan caption itu ya?
      Misal di TreeView menunya adalah:
      Nama Pegawai
      |__Anto
      |__Ridwan

      Begitu diklik di Anto mau menampilkan form detail si Anto. Lebih kurang seperti itukah?

      Kalau iya, lebih kurang seperti ini kodenya:

      procedure TformMain.TreeView1Click(Sender: TObject);
      begin
      formLain := TformLain.Create(Nil);
      formLain.Caption := TreeView1.Selections[0].Text; //intinya yang dipilih di TreeView bisa diakses dari TreeView1.Selections[0].Text
      formLain.ShowModal;
      formLain.Free;
      end;

      Demikian semoga bisa membantu.

  4. Mas kalo treeview bisa dibuat sebagai form input ke database

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: