首页 > 代码库 > 使用 IntraWeb (19) - 基本控件之 TIWTreeView

使用 IntraWeb (19) - 基本控件之 TIWTreeView


这是个饱受非议的控件; 我通过尝试, 理解了非议, 也能理解作者. 总之向作者的思路靠拢吧, 还是不错的.

TIWTreeView 所在单元及继承链:
IWCompTreeview.TIWTreeView < TIWCustomControl < TIWBaseHTMLControl < TIWBaseControl < TIWVCLBaseControl < TControl < TComponent < TPersistent < TObject

主要成员:
property Items: TIWTreeViewItems    //所有 Item 的集合property Selected: TIWTreeViewItem  //在这里, 叫做 "当前 Item" 更合适property AutoIndent: Boolean               //是否自动缩排; 默认 Trueproperty DoServerSidePlusMinus: Boolean    //如果是 True, Html 中不包含被折叠的部分, 需要展开时再从服务器读取; 相反, Html 中包含所有节点; 默认 Falseproperty TreeViewImages: TIWTreeViewImages //通过其 OpenFolderImage、ClosedFolderImage、DocumentImage、PlusImage、MinusImage 子属性指定五个图标property ItemSpace: TIWTreeViewItemSpace //调整纵、横间距property UseItemImages: Boolean          //是否使用 Item 标志图像property TreeViewImagesWidth: Integer    //图标宽度property TreeViewImagesHeigth: Integer   //图标高度property UseFrame: Boolean               //是否使用框架property ScrollToName: string            //滚动到指定名称的 Itemproperty ScrollBars: TIWScrollStyle      //滚动条选项property WrapItems: Boolean              //Item 文本能否换行; 默认 Falseproperty DoRefreshControl: Boolean       //在修改某些属性时必须让它为 True 才会有效property TreeItemClick: TIWTreeViewClickEvent //通过它可以为每个 Item 指定 Click 事件property OnPlusMinus: TTreeViewPlusMinusProc  //每当展开或收拢子节点时procedure ClearAll  //


TIWTreeViewItemsTIWTreeViewItem:
{IWCompTreeview.TIWTreeViewItems < TOwnedCollection < TCollection < TPersistent < TObject}property Count: Integerproperty Items[AIndex: Integer]: TIWTreeViewItemfunction Add(aParentItem: TIWTreeViewItem): TIWTreeViewItemprocedure Clearprocedure Delete(Index: Integer)function Insert(Index: Integer): TCollectionItem{IWCompTreeview.TIWTreeViewItem < TCollectionItem < TPersistent < TObject}property ParentItem: TIWTreeViewItemproperty SubItems: TListproperty UserData: TObjectproperty Caption: stringproperty Expanded: Booleanproperty Font: TIWFontproperty Hint: stringproperty ItemImages: TIWTreeViewImagesproperty Tag: Integerproperty URL: stringproperty Version: Integerproperty Css: stringproperty Collection: TCollectionproperty ID: Integerproperty Index: Integerproperty Name: stringproperty DisplayName: stringproperty OnClick: TNotifyEventprocedure DoClickfunction HasChild: Boolean


测试:

{在空白窗体上放 IWRegion1 和 IWTreeView1; 下面代码在设计时实现会更方便}procedure TIWForm1.IWAppFormCreate(Sender: TObject);var  fItem: TIWTreeViewItem;begin  IWRegion1.Width := 200;  IWRegion1.Align := alLeft;  IWRegion1.Color := clWebYellow;  LinkColor := clWebRed; //如果给 TIWTreeViewItem 指定了 OnClick 事件, 它会被呈现为链接  IWTreeView1.Parent := IWRegion1;  IWTreeView1.Align := alClient;  IWTreeView1.Items.Add(nil).Caption := ‘ItemA‘;  IWTreeView1.Items.Add(nil).Caption := ‘ItemB‘;  IWTreeView1.Items.Add(nil).Caption := ‘ItemC‘;  fItem := IWTreeView1.Items.Add(nil);  fItem.Caption := ‘ItemD‘;    IWTreeView1.Items.Add(fItem).Caption := ‘ItemD1‘;    IWTreeView1.Items.Add(fItem).Caption := ‘ItemD2‘;    fItem := IWTreeView1.Items.Add(fItem);    fItem.Caption := ‘ItemD3‘;      IWTreeView1.Items.Add(fItem).Caption := ‘ItemD31‘;      IWTreeView1.Items.Add(fItem).Caption := ‘ItemD32‘;end;{尽管可以为每个 Item 指定 OnClick, 但我觉得用 TreeItemClick 事件一起指定更方便; 事件中可以通过其 Tag、ID、Name、Caption等来区别}procedure TIWForm1.IWTreeView1TreeItemClick(Sender: TObject; ATreeViewItem: TIWTreeViewItem);begin  if not ATreeViewItem.HasChild then    WebApplication.ShowMessage(ATreeViewItem.Caption);end;


效果图: