zoukankan      html  css  js  c++  java
  • unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, DB, ADODB, StdCtrls, ComCtrls, Grids, DBGrids;

    type
    PNodeRec=^TNodeRec;
    TNodeRec=record
    id: string;
    name: string;
    pid: string;
    end;

    TForm1 = class(TForm)
    TreeView1: TTreeView;
    ADOQuery1: TADOQuery;
    Memo1: TMemo;
    DataSource1: TDataSource;
    ADOQuery2: TADOQuery;
    DBGrid1: TDBGrid;
    ADOConnection1: TADOConnection;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure MakeTree(Query: TADOQuery; TreeView: TTreeView);
    var
    List: TStringList;
    Node: TTreeNode;
    Index: Integer;
    PNode: PNodeRec;
    begin
    TreeView.Items.BeginUpdate;
    try
    TreeView.Items.Clear;
    List := TStringList.Create;
    try
    List.Sorted := True;
    Query.First;
    while not Query.Eof do
    begin
    PNode:= new(PNodeRec);
    PNode^.id := Query.fieldbyname('ID').AsString;
    PNode^.name := Query.fieldbyname('Name').AsString;
    PNode^.pid := Query.fieldbyname('ParentID').AsString;
    if Query.FieldByName('ParentID').AsInteger = 0 then // ParentID=0,root node
    Node := TreeView.Items.AddChild(nil, Query.FieldByName('Name').AsString)
    else
    begin
    Index := List.IndexOf(Query.FieldByName('ParentID').AsString);
    Node := TreeView.Items.AddChild(TTreeNode(List.Objects[Index]),
    Query.FieldByName('Name').AsString);
    end;
    Node.Data := PNode;
    List.AddObject(Query.FieldByName('ID').AsString, Node);
    Query.Next;
    end;
    finally
    List.Free;
    end;
    finally
    TreeView.Items.EndUpdate;
    end;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    Memo1.Clear;
    MakeTree(ADOQuery1, TreeView1);
    end;

    procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
    var
    s: string;

    function blNode(aNode: TTreeNode): string;
    var
    childnode: TTreenode;
    i: Integer;
    begin
    result := ''''+ PNoderec(Node.Data)^.id+''''+',';
    for i :=0 to aNode.Count -1 do
    begin
    childnode := aNode.Item[i];
    Result := Result +''''+PNoderec(childnode.Data)^.id+''''+',';
    if childnode.Count >0 then
    blNode(childnode);
    end;
    Result := Copy(Result, 1, Length(Result)-1);
    end;
    begin
    Memo1.Clear;
    Memo1.Lines.Add('id: '+PNoderec(Node.Data)^.id);
    Memo1.Lines.Add('name: '+PNoderec(Node.Data)^.name);
    Memo1.Lines.Add('pid: '+PNoderec(Node.Data)^.pid);
    s := blNode(Node);
    ADOQuery2.Close;
    ADOQuery2.SQL.Clear;
    ADOQuery2.SQL.Text := 'select a.ProductID,a.ProductName,a.ClassID,b.name as ClassName '+
    'from t2 a inner join t1 b on b.id=a.ClassID '+
    'where a.ClassID in('+s+') or b.parentid in('+s+')' ;
    ADOQuery2.Open;
    end;

    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    var
    node: TTreeNode;
    begin
    node := TreeView1.Items.GetFirstNode;
    while node <> nil do
    begin
    Dispose(PNoderec(node.Data));
    node := node.GetNext;
    end;
    end;

    end.

  • 相关阅读:
    数据结构(2)-链表
    数据结构(1)-数组
    SpringMVC学习总结(一)--Hello World入门
    基本数据类型对象的包装类
    关于String的相关常见方法
    常见的集合容器应当避免的坑
    再一次生产 CPU 高负载排查实践
    分表后需要注意的二三事
    线程池没你想的那么简单(续)
    线程池没你想的那么简单
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2611005.html
Copyright © 2011-2022 走看看