⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 复件 usertreeview.pas

📁 pde专用vcl
💻 PAS
字号:
unit USERTreeView;

interface

uses
  SysUtils, Classes, Controls, ComCtrls, DB, ADODB, Windows;

type
  TUSERTreeView = class(TTreeView)
  private
    { Private declarations }
    FADOconn: TADOConnection;
    FHowShow: string; //0:全部显示;1:显示到部门;2:显示除档案主管外用户;
    function TreeFindItem(NodeItem: TTreeNode; ID: string): TTreeNode;
  protected
    { Protected declarations }
  public
    { Public declarations }
    procedure TreeAddItem(ItemList: TStrings);
    procedure TreeModifyItem(ItemList: TStrings);
    procedure TreeRemoveItem(); overload;
    procedure TreeRemoveItem(ID: string); overload;
    procedure TreeCreate();
    procedure TreeLocation(ID: string);
    procedure TreeMoveItem(ParentID: string; ID: string);

    function GetTreeNodeID(): string; overload;
    function GetTreeNodeUserType(): string; overload;
    function GetTreeNodeLoginName(): string; overload;
    function GetTreeNodeParentID(): string; overload;
    function GetTreeNodeID(ID: string): string; overload;
    function GetTreeNodeUserType(ID: string): string; overload;
    function GetTreeNodeLoginName(ID: string): string; overload;
    function GetTreeNodeParentID(ID: string): string; overload;
  published
    { Published declarations }
    property ADOconn: TADOConnection read FADOconn write FADOconn;
    property HowShow: string read FHowShow write FHowShow;
  end;

procedure Register;

implementation
type
  PTNKEY = ^TTREENODEKEY;
  TTREENODEKEY = record
    ID: string;
    UserType: string;
    LoginName: string;
    ParentID: string;
  end;

procedure Register;
begin
  RegisterComponents('PDE', [TUSERTreeView]);
end;

procedure TUSERTreeView.TreeAddItem(ItemList: TStrings);
var
  NewNode, ThisNode: TTreeNode;
  FKey: PTNKEY;
begin
  New(FKey);
  FKey^.ID := ItemList[0];
  FKey^.UserType := ItemList[1];
  FKey^.LoginName := ItemList[2];
  FKey^.ParentID := ItemList[3];

  Self.Items.BeginUpdate;
  try
    ThisNode := TreeFindItem(nil, ItemList[3]);
    if ThisNode <> nil then
    begin
      NewNode := Self.items.AddChildObject(ThisNode, ItemList[4], FKey);
    end
    else
    begin
      NewNode := Self.items.AddObject(nil, ItemList[4], FKey);
    end;
    //节点类型
    if ItemList[1] = '9' then
    begin
      NewNode.ImageIndex := 0;
      NewNode.SelectedIndex := 0;
    end
    else
    begin
      //是否离职
      if ItemList[5] = '1' then
      begin
        NewNode.ImageIndex := 2;
        NewNode.SelectedIndex := 2;
      end
      else
      begin
        NewNode.ImageIndex := 1;
        NewNode.SelectedIndex := 1;
      end;
    end;
    NewNode.Selected := true;
  finally
    Self.Items.EndUpdate;
  end;
end;

//修改树节点

procedure TUSERTreeView.TreeModifyItem(ItemList: TStrings);
var
  FKey: PTNKEY;
begin
  New(FKey);
  FKey^.ID := ItemList[0];
  FKey^.UserType := ItemList[1];
  FKey^.LoginName := ItemList[2];
  FKey^.ParentID := ItemList[3];

  Self.Items.BeginUpdate;
  try
    Self.Selected.Data := FKey;
    Self.Selected.Text := ItemList[4];
    //节点类型
    if ItemList[1] = '9' then
    begin
      Self.Selected.ImageIndex := 0;
      Self.Selected.SelectedIndex := 0;
    end
    else
    begin
      //是否离职
      if ItemList[5] = '1' then
      begin
        Self.Selected.ImageIndex := 2;
        Self.Selected.SelectedIndex := 2;
      end
      else
      begin
        Self.Selected.ImageIndex := 1;
        Self.Selected.SelectedIndex := 1;
      end;
    end;
  finally
    Self.Items.EndUpdate;
  end;
end;

//移除节点

procedure TUSERTreeView.TreeRemoveItem();
begin
  Self.Items.BeginUpdate;
  try
    Self.Selected.Delete;
  finally
    Self.Items.EndUpdate;
  end;
end;

procedure TUSERTreeView.TreeRemoveItem(ID: string);
var
  STreeNode: TTreeNode;
begin
  Self.Items.BeginUpdate;
  try
    STreeNode := TreeFindItem(nil, ID);
    if not (STreeNode = nil) then
      STreeNode.Delete;
  finally
    Self.Items.EndUpdate;
  end;
end;

//创建树

procedure TUSERTreeView.TreeCreate();
var
  adoquery: TADOQuery;
  itemList: TStrings;
  iLoop: Integer;
  strSql: string;
begin
  try
    adoquery := TADOQuery.Create(self);
    adoquery.Connection := FADOconn;
    if FHowShow = '2' then
      strSql := 'SELECT ID,C_ADMIN,C_LOGINNAME,I_PARENTID,C_REALNAME,I_LEAVE FROM SYS_USERS WHERE I_PARENTID >-1 AND C_ADMIN <> ''3'' AND C_ADMIN <> ''1'' ORDER BY C_ADMIN desc,I_LEVEL,I_PARENTID,C_LOGINNAME'
    else if FHowShow = '1' then
      strSql := 'SELECT ID,C_ADMIN,C_LOGINNAME,I_PARENTID,C_REALNAME,I_LEAVE FROM SYS_USERS WHERE I_PARENTID >-1 AND C_ADMIN = ''9'' ORDER BY C_ADMIN desc,I_LEVEL,I_PARENTID,C_LOGINNAME'
    else if FHowShow = '3' then
      strSql :=
        'SELECT ID,C_ADMIN,C_LOGINNAME,I_PARENTID,C_REALNAME,I_LEAVE FROM SYS_USERS WHERE I_PARENTID >-1 AND (C_ADMIN = ''9'' OR C_ADMIN = ''2'') ORDER BY C_ADMIN desc,I_LEVEL,I_PARENTID,C_LOGINNAME'
    else
      strSql :=
        'SELECT ID,C_ADMIN,C_LOGINNAME,I_PARENTID,C_REALNAME,I_LEAVE FROM SYS_USERS WHERE I_PARENTID >-1 AND C_ADMIN <> ''3'' AND C_ADMIN<>''2'' ORDER BY C_ADMIN desc,I_LEVEL,I_PARENTID,C_LOGINNAME';
    adoquery.SQL.add(strSql);
    adoquery.Open;
    Self.Items.Clear;
    itemList := TStringList.Create;
    while not adoquery.Eof do
    begin
      for iLoop := 0 to adoquery.FieldCount - 1 do
      begin
        itemList.Add(adoquery.Fields[iLoop].AsString);
      end;
      TreeAddItem(itemList);
      itemList.Clear;
      adoquery.Next;
    end;
    adoquery.Close;
    adoquery.Destroy;
    if Self.Items.Count > 0 then
    begin
      Self.Items.Item[0].Selected := true;
      Self.FullCollapse;
      Self.Items.Item[0].Expand(false);
    end;
  except
    on E: Exception do
      Messagebox(self.Handle,
        PAnsiChar('数据库操作失败!请检查数据连接是否正常。' + chr(10) + chr(13)
        +
        '详细错误信息如下:' + chr(10) + chr(13) + E.Message),
        PAnsiChar('信息'),
        MB_OK + MB_ICONINFORMATION);
  end;
end;

//移动节点

procedure TUSERTreeView.TreeMoveItem(ParentID: string; ID: string);
begin
  if TreeFindItem(nil, ID).IsFirstNode then
    Exit;
  if ParentID = PTNKEY(TreeFindItem(nil, ID).Parent.Data)^.ID then
    Exit;
  TreeFindItem(nil, ID).MoveTo(TreeFindItem(nil, ParentID), naAddChild);
end;

//查找父节点

function TUSERTreeView.TreeFindItem(NodeItem: TTreeNode; ID: string): TTreeNode;
begin
  if NodeItem = nil then
    NodeItem := Self.items.getfirstnode
  else
    NodeItem := NodeItem.getfirstchild;
  if (NodeItem <> nil) and (PTNKEY(NodeItem.Data)^.ID <> ID) then
    repeat
      NodeItem := NodeItem.GetNext;
    until (NodeItem = nil) or (PTNKEY(NodeItem.Data)^.ID = ID);
  Result := NodeItem;
end;

procedure TUSERTreeView.TreeLocation(ID: string);
begin
  TreeFindItem(nil, ID).Selected := true;
end;

//得到节点ID值

function TUSERTreeView.GetTreeNodeID(): string;
begin
  if Self.Items.Count = 0 then
    Result := '0'
  else
    Result := PTNKEY(Self.Selected.Data)^.ID;
end;

function TUSERTreeView.GetTreeNodeID(ID: string): string;
begin
  if Self.Items.Count = 0 then
    Result := '0'
  else
    Result := PTNKEY(TreeFindItem(nil, ID).Data)^.ID;
end;
//得到节点UserType值

function TUSERTreeView.GetTreeNodeUserType(): string;
begin
  if Self.Items.Count = 0 then
    Result := ''
  else
    Result := PTNKEY(Self.Selected.Data)^.UserType;
end;

function TUSERTreeView.GetTreeNodeUserType(ID: string): string;
begin
  if Self.Items.Count = 0 then
    Result := ''
  else
    Result := PTNKEY(TreeFindItem(nil, ID).Data)^.UserType;
end;
//得到节点LoginName值

function TUSERTreeView.GetTreeNodeLoginName(): string;
begin
  if Self.Items.Count = 0 then
    Result := ''
  else
    Result := PTNKEY(Self.Selected.Data)^.LoginName;
end;

function TUSERTreeView.GetTreeNodeLoginName(ID: string): string;
begin
  if Self.Items.Count = 0 then
    Result := ''
  else
    Result := PTNKEY(TreeFindItem(nil, ID).Data)^.LoginName;
end;
//得到节点ParentID值

function TUSERTreeView.GetTreeNodeParentID(): string;
begin
  if Self.Items.Count = 0 then
    Result := ''
  else
    Result := PTNKEY(Self.Selected.Data)^.ParentID;
end;

function TUSERTreeView.GetTreeNodeParentID(ID: string): string;
begin
  if Self.Items.Count = 0 then
    Result := ''
  else
    Result := PTNKEY(TreeFindItem(nil, ID).Data)^.ParentID;
end;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -