菜单树
Posted 咏南中间件和开发框架
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了菜单树相关的知识,希望对你有一定的参考价值。
菜单树
// add by cxg 2017-4-18
unit uTree;
interface
uses
SysUtils, Classes, ComCtrls, DB, DBClient;
type
PNodeRec = ^TNodeRec;
TNodeRec = record // 树节点记录
id: string;
name: string;
pid: string;
formname: string; // add by cxg 2017-2-14
bplname: string; // add by cxg 2017-4-18
end;
procedure CreateTree(dataset: TClientDataSet; treeview: TTreeView; id, aname, parentid: string);
procedure FreeTree(treeview: TTreeView);
implementation
procedure CreateTree(dataset: TClientDataSet; treeview: TTreeView; id, aname, parentid: string);
// table struct example: id,name,pid
var
List: TStringList;
Node: TTreeNode;
Index: Integer;
PNode: PNodeRec;
begin
if not dataset.Active or dataset.IsEmpty then
Exit;
treeview.Items.Clear;
List := TStringList.Create;
try
List.Sorted := True;
dataset.First;
while not dataset.Eof do
begin
PNode := new(PNodeRec);
PNode^.id := dataset.FieldByName(id).Text;
PNode^.name := dataset.FieldByName(aname).Text;
PNode^.pid := dataset.FieldByName(parentid).Text;
// add by cxg 2017-2-14
if dataset.FindField(‘formname‘) <> nil then
PNode.formname := dataset.FindField(‘formname‘).Text;
// add by cxg 2017-4-18
if dataset.FindField(‘bplname‘) <> nil then
PNode.bplname := dataset.FindField(‘bplname‘).Text;
if (dataset.FieldByName(parentid).Text = ‘‘) or (dataset.FieldByName(id).Text = dataset.FieldByName(parentid).Text) then
// add root node
Node := treeview.Items.AddChild(nil, dataset.FieldByName(aname).Text)
else
begin
// add child node
Index := List.IndexOf(dataset.FieldByName(parentid).Text);
Node := treeview.Items.AddChild(TTreeNode(List.Objects[Index]), dataset.FieldByName(aname).Text);
end;
Node.Data := PNode;
List.AddObject(dataset.FieldByName(id).Text, Node);
dataset.Next;
end;
finally
List.Free;
end;
end;
procedure FreeTree(treeview: TTreeView);
var
i: Integer;
Node: TTreeNode;
begin
for i := treeview.Items.Count - 1 downto 0 do
begin
Node := treeview.Items[i];
Dispose(PNodeRec(Node.Data));
end;
end;
end.
以上是关于菜单树的主要内容,如果未能解决你的问题,请参考以下文章