• DataSetToTreeView


    {
     Copyright (c) 咏南工作室
     Author:       陈新光
       Date:       2006.11.26
       ToDo:       类别、部组业务单元
    }

    unit uTree;

    interface

    uses
      sysUtils, comCtrls, db, dbClient,
      forms,
      iTree,      //接口单元
      uDm2;       //数据模块

    type
      Ttree = class(TinterfacedObject, It)
      private
        dm2: Tdm2;
        {增加节点}
        procedure AppendNode(Tree: TTreeView; NODE: TTreeNode;
          cds: TclientDataSet);
        {查找节点}
        function FindNode(Tree: TTreeView; s: string): TTreeNode;
      public
        constructor create;
        destructor destroy; override;
        {读取数据生成树型控件}
        procedure DrawTree(Tree: Ttreeview; cds: TclientDataSet);
        {得到类别数据集}
        function GetType: oleVariant;
        {保存类别}
        procedure saveType(data: oleVariant; out ErrCount: integer);
        {类别树型控件的OnClick事件代码}
        procedure typeClick(node: TTreeNode; cds: TclientDataSet);
      end;

    implementation

    { Ttree }

    constructor Ttree.create;
    begin
      dm2 := Tdm2.Create(nil);
    end;

    destructor Ttree.destroy;
    begin
      freeandnil(dm2);
      inherited;
    end;

    function Ttree.FindNode(TREE: TTreeView; s: string ): TTreeNode;
    var
      i: Integer;
      P: Pstring;
    begin
      result := nil;
      for i := 0 to tree.items.count - 1  do
      begin
        P := Tree.Items[i].Data;
        if P^ = s then
        begin
          result := Tree.Items[i];
          Exit;
        end;
      end;
    end;

    procedure Ttree.DrawTree(Tree: Ttreeview; cds: TclientDataSet);
    var
      i: Integer;
      node, pnode: TtreeNode;
    begin
      Tree.Items.Clear;
      Tree.Items.BeginUpdate;
      node := Tree.Items.GetFirstNode;
      for i := 0  to cds.RecordCount - 1 do
      begin
        {if type_from_id = '0' then it is root node}
        if cds.Fields[2].Text  = '0' then
          AppendNode(Tree, NODE, cds)
        else
        begin
          pnode := FindNode(Tree, cds.Fields[2].Text);
          if pnode <> nil then
            AppendNode(Tree, pnode, cds);
        end;
        cds.Next;
      end;
      Tree.Items.EndUpdate;
    end;

    procedure Ttree.AppendNode(Tree: TTreeView; NODE: TTreeNode;
      cds: TclientDataSet);
    var
      CatNode: TTreeNode;
      P: PString;
    begin
      CatNode := Tree.Items.AddChild(NODE, cds.Fields[1].Text + '(' +
        cds.Fields[0].Text + ')');
      New(P);
      P^ := cds.Fields[0].Text ;
      CatNode.Data := P;
    end;

    function Ttree.GetType: oleVariant;
    begin
      if not dm2.ac.Connected then
        dm2.ac.Connected := true;
      dm2.adqType.Open;
      result := dm2.dspType.Data;
      dm2.adqType.Close;
      dm2.ac.Close;
    end;

    procedure Ttree.saveType(data: oleVariant; out ErrCount: integer);
    begin
      dm2.dspType.ApplyUpdates(data, 0, ErrCount);
    end;

    procedure Ttree.typeClick(node: TTreeNode; cds: TclientDataSet);
    var
      pid: pstring;
    begin
      pid := Node.DATA;
      if not dm2.ac.Connected then
        dm2.ac.Open;
      with dm2.adqType do
      begin
        close;
        sql.Clear;
        sql.Text := ' select * from type WHERE type_id= ''' + pid^ + ''' ';
        open;
      end;
      cds.Data := dm2.dspType.Data;
      dm2.adqType.Close;
      dm2.ac.Close;
    end;

    end.


    {
     Copyright (c) 咏南工作室
     Author:       陈新光
       Date:       2006.11.26
       ToDo:       类别、部组接口单元
    }

    unit iTree;

    interface

    uses
      db, dbClient, comCtrls;

    type
      It = interface
        {读取数据生成树型控件}
        procedure DrawTree(Tree: Ttreeview; cds: TclientDataSet);
        {得到类别数据集}
        function GetType: oleVariant;
        {保存类别}
        procedure saveType(data: oleVariant; out ErrCount: integer);
        {类别树型控件的Onchange事件代码}
        procedure typeClick(node: TTreeNode; cds: TclientDataSet);
      end;

    implementation

    end.

    类别数据表设计

    --类别
    create table type (
            type_id varchar (50) primary key ,
            type_name varchar (100) not null ,
            type_from_id varchar (8) not null 

  • 相关阅读:
    数据结构(树链剖分):NOI2014 购票
    数据结构(树链剖分):COGS 2109. [NOIP2015] 运输计划
    数据结构(树链剖分,堆):HNOI 2016 network
    快速傅里叶变换(FFT):COGS 2216. 你猜是不是KMP
    生成树的计数(基尔霍夫矩阵):BZOJ 1002 [FJOI2007]轮状病毒
    数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo
    数位DP:SPOJ KPSUM
    动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
    数据结构(并查集):COGS 260. [NOI2002] 银河英雄传说
    生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2941043.html
Copyright © 2020-2023  润新知