DBSelfTree.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:5k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit dbselftree;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   DBTables, Db, fcdbtreeview, Grids,
  6.   StdCtrls, Mask, DBCtrls, ComCtrls, fccommon, fcCombo, fctreecombo,
  7.   ExtCtrls, fctreeheader, DBGrids, fcDemoRichEdit, fcLabel, ImgList,
  8.   Provider, DBClient, fcImager;
  9. type
  10.   TSelfDBForm = class(TForm)
  11.     ScrollBox1: TScrollBox;
  12.     fcDBTreeView1: TfcDBTreeView;
  13.     Panel1: TPanel;
  14.     RootNodesDataSource: TDataSource;
  15.     RootNodesQuery: TQuery;
  16.     ChildNodesDataSource: TDataSource;
  17.     ChildNodesQuery: TQuery;
  18.     IndexLookupTable: TTable;
  19.     ViewDataSource: TDataSource;
  20.     ViewTable: TTable;
  21.     Database1: TDatabase;
  22.     HeaderControl1: TfcTreeHeader;
  23.     DBRichEdit1: TDBRichEdit;
  24.     ViewTableMsgID: TAutoIncField;
  25.     ViewTableMsgIDString: TStringField;
  26.     ViewTableDate: TDateTimeField;
  27.     ViewTableRootID: TIntegerField;
  28.     ViewTableParentID: TIntegerField;
  29.     ViewTableSubject: TStringField;
  30.     ViewTableName: TStringField;
  31.     ViewTableMsg: TMemoField;
  32.     fcLabel1: TfcLabel;
  33.     fcLabel2: TfcLabel;
  34.     fcLabel3: TfcLabel;
  35.     fcLabel4: TfcLabel;
  36.     ImageList1: TImageList;
  37.     RootNodesClientDataSet: TClientDataSet;
  38.     RootNodesProvider: TDataSetProvider;
  39.     RootNodesClientDataSetMsgID: TAutoIncField;
  40.     RootNodesClientDataSetDate: TDateTimeField;
  41.     RootNodesClientDataSetRootID: TIntegerField;
  42.     RootNodesClientDataSetParentID: TIntegerField;
  43.     RootNodesClientDataSetSubject: TStringField;
  44.     RootNodesClientDataSetName: TStringField;
  45.     TileImager: TfcImager;
  46.     procedure fcDBTreeView1UserExpand(TreeView: TfcDBCustomTreeView;
  47.       Node: TfcDBTreeNode);
  48.     procedure fcDBTreeView1CalcNodeAttributes(
  49.       TreeView: TfcDBCustomTreeView; Node: TfcDBTreeNode);
  50.     procedure fcDBTreeView1Change(TreeView: TfcDBCustomTreeView;
  51.       Node: TfcDBTreeNode);
  52.     procedure HeaderControl1SectionClick(HeaderControl: TfcTreeHeader;
  53.       Section: TfcTreeHeaderSection);
  54.     procedure FormShow(Sender: TObject);
  55.   private
  56.      LastDS: TDataSource;
  57.      OrderByFieldName: string;
  58.      OldSection: TfcTreeHeaderSection;
  59.     { Private declarations }
  60.   public
  61.     { Public declarations }
  62.   end;
  63. var
  64.   SelfDBForm: TSelfDBForm;
  65. implementation
  66. {$R *.DFM}
  67. procedure TSelfDBForm.fcDBTreeView1UserExpand(TreeView: TfcDBCustomTreeView;
  68.   Node: TfcDBTreeNode);
  69. var childquery: TQuery;
  70.     childdatasource: TDatasource;
  71. begin
  72.    if fcdbtreeview1.displayfields.count=0 then
  73.    begin
  74.        fcdbtreeview1.displayfields.add('');
  75.        fcdbtreeview1.displayfields.add('');
  76.    end;
  77.    if node.level+1<fcdbtreeview1.displayfields.count then exit;
  78.    { Create new detail paramaterized query }
  79.    childdatasource:= TDatasource.create(self);
  80.    childdatasource.name:= 'ChildDataSource' + inttostr(node.level+1);
  81.    childquery:= TQuery.create(self);
  82.    childdatasource.dataset:= childquery;
  83.    with childquery do begin
  84.       childquery.sql.assign(ChildNodesQuery.sql);
  85.       childquery.databasename:= ChildNodesQuery.databasename;
  86.       if lastds=nil then childquery.datasource:= ChildNodesDataSource
  87.       else childquery.datasource:= lastDS;
  88.       childquery.active:=true;
  89.    end;
  90.    with fcdbtreeview1 do begin
  91.      DataSources:= DataSources + ';'+ ChildDatasource.name;
  92.      displayfields.add(displayfields[displayfields.count-1]);
  93.      lastDS:= Childdatasource;
  94.    end
  95. end;
  96. procedure TSelfDBForm.fcDBTreeView1CalcNodeAttributes(
  97.   TreeView: TfcDBCustomTreeView; Node: TfcDBTreeNode);
  98. begin
  99.    { Determine if expand button should be displayed by using a lookup table }
  100.    node.haschildren:= IndexLookupTable.Locate('ParentID', node.dataset.fieldbyname('msgid').asstring, []);
  101. end;
  102. procedure TSelfDBForm.fcDBTreeView1Change(TreeView: TfcDBCustomTreeView;
  103.   Node: TfcDBTreeNode);
  104. begin
  105.    { Synchronize table bound to edit controls }
  106.    ViewTable.locate('MsgID', node.dataset.fieldbyname('MsgID').asstring, []);
  107. end;
  108. // Sort on column click
  109. procedure TSelfDBForm.HeaderControl1SectionClick(HeaderControl: TfcTreeHeader;
  110.   Section: TfcTreeHeaderSection);
  111. begin
  112.    if section.fieldname='' then exit;
  113.    screen.cursor:= crhourglass;
  114.    OrderByFieldName:= Section.FieldName;
  115.    // Update active section image
  116.    Section.ImageIndex:=0;
  117.    if Section<>OldSection then
  118.    begin
  119.      OldSection.ImageIndex:= -1;
  120.      OldSection:= Section;
  121.    end;
  122.    RootNodesClientDataSet.IndexFieldNames:= section.fieldname;
  123.    screen.cursor:= crarrow;
  124.    // Return to root active node, as otherwise the tree could be confused if
  125.    // there are no children in root
  126.    fcdbtreeview1.MakeActiveDataSet(RootNodesClientDataSet, true);
  127. //   fcdbtreeview.activedataset:= RootNodesDataSet;
  128. {   exit;
  129.    with RootNodesQuery do begin
  130.       sql.clear;
  131.       sql.add('Select * from fcmsg');
  132.       sql.add('where fcmsg."parentID" is null');
  133.       sql.add('order by fcmsg."' + Section.FieldName + '"');
  134.       active:= false;
  135.       active:= true;
  136.    end;
  137.    screen.cursor:= crarrow;
  138. }
  139. end;
  140. procedure TSelfDBForm.FormShow(Sender: TObject);
  141. begin
  142.      OldSection:= HeaderControl1.Sections[0];
  143. end;
  144. end.