我做了一个关于动态增加、修改、删除树形结构

页面导航:首页 > 软件编程 > Delphi > 我做了一个关于动态增加、修改、删除树形结构

我做了一个关于动态增加、修改、删除树形结构

来源: 作者: 时间:2016-01-21 09:33 【

procedure LoadTree(treeDB:TDBDataSet);//初始化树 procedure UpdateTree(curNode:TTreenode; nodeTxt:string; state:string);//更新树 function GetNodeLevel(sFormat,s

    procedure LoadTree(treeDB:TDBDataSet);//初始化树
    procedure UpdateTree(curNode:TTreenode; nodeTxt:string; state:string);//更新树
    function  GetNodeLevel(sFormat,sCode:string):integer; 得节点层数
    function  GetNodeItem(sCode:string):integer;//获得item

    { Public declarations }
  end;

    const
  CTreeCodeFormat=122222;
  cTreeMaxLevel=6;
  CTreeRootTXT=所有;

var
  tsgzlfrom: Ttsgzlfrom;
  _err:integer;
  curUser:string[10];
  mystate:string;
  gNodeId:string;
  gNodelevel:integer;
  gNode:TtreeNode;
  mynode:array[0..6] of TTreenode;
  i,Already,CurMode:integer;
  currow:integer;
  iniFile:string;
  HasSub:String;

  level:Integer;


  implementation

uses bgNewunit;


{$R *.DFM}

procedure TtsgzlFrom.LoadTree(treeDB:TDBDataSet);//初始化树
var curID,nodeTxt:string;
    level,num:integer;
begin
    始化变量
    Screen.Cursor:=crHourGlass;
    tree.Enabled:=True;
    tree.Items.Clear;
    level:=1 ;
    num:=1;
    tree.items.clear;
    置根节点
    mynode[level]:=Tree.items.add(Tree.Topitem,cTreeRootTxt);
    mynode[level].ImageIndex:=0;
    mynode[level].SelectedIndex:=1;
    历数据表,利用编码字段记录排序规律,依次添加树节点
    with TreeDb do
    begin
       try
       if not Active then open;
       first;
       while not Eof do
       begin
         curID:=trim(FieldByName(tsglb).AsString);
         nodeTxt:=curID+-+trim(FieldByName(tsglbn).AsString);
         level:=GetNodeLevel(cTreeCodeFormat,curID);
         里返回代码的层次数
         if level>0 then
         begin
  加下一节点时,用添加子节点的方法可轻松实现节点间的层次关系。
         意:这里的父节点是用当前节点的上一级节点mynode[level-1]
            mynode[level]:=Tree.Items.AddChild(Mynode[level-1],NodeTxt);
            mynode[level].ImageIndex:=2;
            mynode[level].SelectedIndex:=3;
         end;
         next;//下一条记录
       end;
       finally;
         close;
       End;
       mynode[1].expand(False);
       Screen.Cursor:=crHourGlass;
     end;
end;

function TtsgzlFrom.GetNodeLevel(sFormat,sCode:string):integer;
var i,iLen:integer;
begin
  level:=-1  ;
  iLen:=0;
  if (sFormat<>) and (sCode<>) then
    for i:=1 to Length(sFormat) do 析编码格式,找出当前代码层次
    begin
      iLen:=iLen+StrToInt(sFormat[i]);
      if Length(sCode)=iLen then
      begin
        level:=i;
        break;
      end;
    end;
  result:=level;
end;

Function TtsgzlFrom.GetNodeItem(sCode:string):integer;//获得item
var i,iCount,val:integer;
    tmp:string;
begin
  Result:=0;
  iCount:=Tree.Items.Count;
  if iCount=0 then exit;
  val:=0;
  for i:=1 to iCount-1 do
  begin
     Tmp:=Tree.Items.Item[i].Text;
     Tmp:=Copy(Tmp,0,pos(-,Tmp)-1);
     if Tmp=sCode then begin
        val:=i;
        Break;
     end;
  end;
  result:=val;
end;

 

procedure TtsgzlFrom.UpdateTree(curNode:TTreenode; nodeTxt:string; state:string);
Begin
  if  state=add   then
    begin
      curNode:=Tree.Items.addchild(curNode,nodeTxt);
      curNode.ImageIndex:=2;
      curnode.SelectedIndex:=3;
    end;
  if state=del then  curNode.delete;
  if state=edi then  curNode.Text:=nodeTxt;
end;

Tags:

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <