程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 關於動態增加、修改、刪除樹形結構的程序

關於動態增加、修改、刪除樹形結構的程序

編輯:Delphi
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;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved