unigui TUniTreeView demo

unit untTree;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
uniGUIClasses, uniGUIFrame, uniGUIBaseClasses, uniToolBar, uniTreeView,
Data.DB, Datasnap.DBClient, untEditBase, untDll;

type
PNodeRec = ^TNodeRec;

TNodeRec = record
id: string;
name: string;
pid: string;
end;

type
TfrmTree = class(TUniFrame)
UniToolBar1: TUniToolBar;
btnQuery: TUniToolButton;
btnAppend: TUniToolButton;
btnEdit: TUniToolButton;
btnDelete: TUniToolButton;
ClientDataSet1: TClientDataSet;
UniTreeView1: TUniTreeView;
btnExpand: TUniToolButton;
btnCollapse: TUniToolButton;
procedure btnExpandClick(Sender: TObject);
procedure btnCollapseClick(Sender: TObject);
procedure UniTreeView1Click(Sender: TObject);
private
{ Private declarations }
protected
procedure append(f: TfrmEditBase);
function Del(const table: string): Boolean;
procedure edit(f: TfrmEditBase);
procedure Query(const sql: string);
public
{ Public declarations }
end;

procedure CreateTree(dataset: TClientDataSet; treeview: TUniTreeView);
procedure FreeTree(treeview: TUniTreeView);

implementation

{$R *.dfm}
{ TfrmTree }

procedure CreateTree(dataset: TClientDataSet; treeview: TUniTreeView);
// table struct example: id,name,pid
var
List: TStringList;
Node: TUniTreeNode;
Index: Integer;
PNode: PNodeRec;
begin
treeview.Items.Clear;
List := TStringList.Create;
try
List.Sorted := True;
dataset.First;
while not dataset.Eof do
begin
PNode := new(PNodeRec);
PNode^.id := dataset.Fields[0].Text;
PNode^.name := dataset.Fields[1].Text;
PNode^.pid := dataset.Fields[2].Text;
if (dataset.Fields[2].Text = '') or
(dataset.Fields[0].Text = dataset.Fields[2].Text) then
// add root node
Node := treeview.Items.AddChild(nil, dataset.Fields[1].Text)
else
begin
// add child node
Index := List.IndexOf(dataset.Fields[2].Text);
Node := treeview.Items.AddChild(TUniTreeNode(List.Objects[Index]),
dataset.Fields[1].Text);
end;
Node.Data := PNode;
List.AddObject(dataset.Fields[0].Text, Node);
dataset.Next;
end;
finally
List.Free;
end;
end;

procedure FreeTree(treeview: TUniTreeView);
var
i: Integer;
Node: TUniTreeNode;
begin
for i := treeview.Items.Count - 1 downto 0 do
begin
Node := treeview.Items[i];
Dispose(PNodeRec(Node.Data));
end;
end;

procedure TfrmTree.append(f: TfrmEditBase);
begin
ClientDataSet1.append;
if UniTreeView1.Selected <> nil then
begin
ClientDataSet1.Fields[0].Text := PNodeRec(UniTreeView1.Selected.Data)^.id;
ClientDataSet1.Fields[2].Text := PNodeRec(UniTreeView1.Selected.Data)^.id;
end;
f.FClientDataSet := ClientDataSet1;
f.ShowModal(
procedure(Res: Integer)
begin
CreateTree(ClientDataSet1, UniTreeView1);
end);
end;

procedure TfrmTree.btnCollapseClick(Sender: TObject);
begin
UniTreeView1.FullCollapse;
end;

procedure TfrmTree.btnExpandClick(Sender: TObject);
begin
UniTreeView1.FullExpand;
end;

function TfrmTree.Del(const table: string): Boolean;
begin
if ClientDataSet1.IsEmpty then
Exit;
MessageDlg('是否删除?', mtConfirmation, mbYesNo,
procedure(Res: Integer)
begin
if Res = mrYes then
begin
ClientDataSet1.Delete;
if untDll.SaveData(table, ClientDataSet1.Delta) then
begin
ClientDataSet1.MergeChangeLog;
ShowMessage('删除成功');
end
else
ShowMessage('删除失败');
end;
end);
end;

procedure TfrmTree.edit(f: TfrmEditBase);
begin
if ClientDataSet1.IsEmpty then
Exit;
ClientDataSet1.edit;
f.FClientDataSet := ClientDataSet1;
f.ShowModal(
procedure(Res: Integer)
begin
CreateTree(ClientDataSet1, UniTreeView1);
end);
end;

procedure TfrmTree.Query(const sql: string);
begin
ClientDataSet1.Data := untDll.QuerySql(sql);
if ClientDataSet1.IsEmpty then
Exit;
CreateTree(ClientDataSet1, UniTreeView1);
end;

procedure TfrmTree.UniTreeView1Click(Sender: TObject);
begin
if not ClientDataSet1.IsEmpty then
ClientDataSet1.Locate(ClientDataSet1.Fields[0].FieldName,
VarArrayOf([PNodeRec(UniTreeView1.Selected.Data)^.id]), []);
end;

end.

posted @ 2015-04-11 21:21  delphi中间件  阅读(2550)  评论(0编辑  收藏  举报