关闭

TreeView通过读数据库里的记录创建节点

1006人阅读 评论(0) 收藏 举报

把菜单项都存在一个数据表里,这个数据表可以有无限级,通过读这个数据表来创建 Treeview 的节点
同时包括对节点的增删改


表结构

  create table HRDict( 
  id                int identity(1,1), 
          ModuleName        char(12), 
  ParentId          int 为 -1 则代表是根
  ) 

//-----------------------------------------------------


unit DictUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, StdCtrls,dbtables;

type
  TNodeId = record
    id : integer;
  end;
  TDictForm = class(TForm)
    Panel1: TPanel;
    TreeView1: TTreeView;
    edContent: TEdit;
    btnNewSave: TButton;
    btnModifySave: TButton;
    btnRemove: TButton;
    btnClose: TButton;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnNewSaveClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btnModifySaveClick(Sender: TObject);
    procedure btnRemoveClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
  private
    procedure showNodes();             // 显示节点
    procedure CreateChildNode(ParentNode:TTreeNode;parentID:integer);         // 建立子节点

    procedure NewSave(isSameLevel:boolean);
    procedure ModifySave();
    procedure DeleteSave();
  public
    { Public declarations }
  end;

var
  DictForm: TDictForm;

implementation

uses DMUnit;

{$R *.dfm}

procedure TDictForm.showNodes();
var
  node:TTreeNode;
  nodeId:TNodeid;
begin
  self.TreeView1.Items.Clear;
  with DmForm.HRDictQry do
  begin
    if active then close;
    sql.Clear;
    sql.Add('select * from HRDict where parentId=-1');
    open;
    if recordcount=0 then exit;

    while not eof do
    begin
      node := treeView1.Items.Add(nil,FieldByName('moduleName').AsString);
      nodeID.id := FieldbyName('id').AsInteger;
      node.Data := Pointer(nodeid.id);
      self.CreateChildNode(node,FieldByName('id').AsInteger);
      next;
    end;
    close;

  end;
 
end;

procedure TDictForm.CreateChildNode(parentNode:TTreeNode;ParentID:integer);
var
  tempQry : TQuery;
  node:TTreeNode;
  nodeID:TNodeID;
begin
  tempQry := TQuery.Create(nil);
  tempQry.DatabaseName := DMForm.HrmsDataBase.DatabaseName;
  try
    with tempQry do
    begin
      sql.Add('select * from HRDict where parentid='+IntToStr(parentid));
      open;
      if recordcount=0 then exit;
      while not eof do
      begin
        node := self.TreeView1.Items.AddChild(parentnode,
        FieldByName('modulename').AsString);
        nodeId.id := FieldByName('id').AsInteger;
        node.Data := pointer(nodeid.id);
        self.CreateChildNode(node,FieldByName('id').AsInteger);
        next;
      end;
    end;
  finally
    tempQry.Free;
  end;
end;

procedure TDictForm.NewSave(isSameLevel:boolean);
var
 //pid:integer;        // parentId
 id :integer;
begin
  if trim(self.edContent.Text)='' then exit;

  id := integer(treeview1.Selected.Data);
  with DMform.HRDictQry do
  begin
    if isSameLevel then      // 是同一级别
    begin
      if active then close;
      sql.Clear;
      sql.Add('select parentid from HRDict where id='+IntToStr(id));
      open;
      id := Fields[0].AsInteger;
    end;
    close;
    sql.Clear;
    sql.Add('insert HRDict(moduleName,parentid)values'
    +'('''+self.edContent.Text+''','
    +inttostr(id)+')');
    execsql;
    self.showNodes;
  end;
end;

procedure TDictForm.ModifySave;
var
  id :integer;
  ss :string;
begin
  id := integer(self.TreeView1.Selected.Data);
  ss := trim(self.edContent.Text);
  if ss = '' then exit;
  with DMForm.HRDictQry do
  begin
    if active then close;
    sql.Clear;
    sql.Add('update hrdict set moduleName='''+ss+''' where id='+intTostr(id));
    execsql;
    self.showNodes;
  end;
end;

procedure TDictForm.DeleteSave;
var
  id :integer;
begin
  if messagebox(0,pchar('确认要删除吗?'),pchar('提示'),mb_yesno+mb_iconquestion)=mryes then
  begin
    id := integer(self.TreeView1.Selected.Data);
    with DMForm.HRDictQry do
    begin
      if active then close;
      sql.Clear;
      sql.Add('delete hrdict  where id='+intTostr(id)+' or parentid='+inttostr(id));
      execsql;
      self.showNodes;
    end;
  end;
end;

procedure TDictForm.FormCreate(Sender: TObject);
begin
  self.showNodes;
end;

procedure TDictForm.btnNewSaveClick(Sender: TObject);
begin
  self.NewSave(true);
end;

procedure TDictForm.Button1Click(Sender: TObject);
begin
  self.NewSave(false);
end;

procedure TDictForm.btnModifySaveClick(Sender: TObject);
begin
  self.ModifySave;
end;

procedure TDictForm.btnRemoveClick(Sender: TObject);
begin

  self.DeleteSave;
end;

procedure TDictForm.btnCloseClick(Sender: TObject);
begin
  close;
end;

end.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:250988次
    • 积分:3807
    • 等级:
    • 排名:第8691名
    • 原创:109篇
    • 转载:70篇
    • 译文:0篇
    • 评论:18条
    最新评论
    链接
    上位机