• 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 

  • 相关阅读:
    ES6 promise的用法
    elementUI el-dialog 解决embed标签无法实时更新预览PDF文件
    elementUI upload 上传 传参
    elementUI form表单验证
    Ajax 原理过程 同步与异步区别 优缺点
    《深入理解mybatis原理(十二)》 mybatis深入理解之#与$区别
    截取字符串,含有汉字和字母,不能截半个汉字
    输出一个纯英文字符串中出现次数最左的单词和次数,如果有几个次数最多的单词,全部输出
    极简主义生活方式
    MySQL中创建表指定存储引擎
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2941043.html
Copyright © 2020-2023  润新知