gpt4 book ai didi

Delphi cxtreelist循环遍历节点

转载 作者:行者123 更新时间:2023-12-01 23:34:17 35 4
gpt4 key购买 nike

我有一个 tcxTreeList,有 3 列,第一列设置了属性Node.CheckGroupType := ncgCheckGroup;,第二列包含第一个根节点的Key,第三列包含根节点下所有子节点的Key。我如何获取根节点的 key (无论是否选中)以及所有选中的子节点的 key 到2个变量中以插入到数据库中并从我们从数据库获取的 key 加载回项目 enter image description here

我添加项目的代码如下

 APNode := tv.Add;
APNode.CheckGroupType := ncgCheckGroup;
APNode.Values[0] := define;
ApNode.Values[1] := dCode;

define - 包含“已创建”、“已发布”、“不活动”等文本。所有根节点dcode - 包含每个根节点的键值,如 E、F、I、P、R

这就是我为每个根节点添加子节点的方式

procedure TF_Form1.addPStatsToTreeList(tl: TcxTreeList; const dcode, define: string);
function AddTreeListNode(TreeList: TcxTreeList; APNode: TcxTreeListNode; const AValues: Array of Variant; AImageIndex: Integer): TcxTreeListNode;
begin
Result := TreeList.AddChild(APNode);
Result.AssignValues(AValues);
Result.Imageindex := AImageIndex;
end;
var
grpnode,chNode, ANode: TcxTreeListNode;
icnt : integer;
begin
icnt := tl.Count;
if Assigned(tl) then
begin
ANode := tl.TopNode;
while ANode <> nil do
begin
ANode := AddTreeListNode(tl, ANode, [define, '', dcode], 0);
ANode := TcxTreeListNode(ANode.GetNext);
end;
end;
end;

更新

dcode 是单个字母,Definition 是字符串,Dcode 是定义中字符串的键,就像键值对一样,Dcode 是键,定义是值。在树列表中,根节点(例如“已创建”、“已发布”、“处于事件状态”、“已检查”和“已重新激活”)具有不同的键,位于第 1 列中,子节点值(例如“样本”、“开发”、“生产”等)的键位于第 3 列中。仅将Key值保存到DB中,根节点键无论是否Checked都直接保存。每个根节点 key 将保存到不同的行,并且仅到达子节点。检查的节点 key 将保存到数据库中,并以逗号分隔。例如 。如果创建了根节点并且选中了其下的项目(例如 Sample 、 Development ),则根节点键 E 是一列 [DEFCODE(Primarykey)] ,子节点键 M,E (DEFINITION ) 用逗号分隔在其他列中。

最佳答案

下面的代码显示了如何将根节点的已检查状态及其键及其子键的逗号分隔列表,使用以下命令检查SaveTreeState 方法。

LoadTreeState方法清除ctTreeList中每个节点的Checked状态然后从 CDS 重新加载保存的 Checked 状态。

要使用此代码,您需要将名为 CDS1 的 TClientDataSet 添加到您的项目中,TDataSource 和 TDBGrid 以通常的方式连接。然后添加持久化将字段添加到CDS中,如代码所示,编译并运行。

我希望我已经理解你的 cxTreeList 是如何正确设置的,但如果没有,希望代码应该非常简单,可以适应您实际所做的事情。

type
TForm1 = class(TForm)
cxTreeList1: TcxTreeList;
CDS1: TClientDataSet;
btnLoad: TButton;
cxTreeList1Column1: TcxTreeListColumn;
cxTreeList1Column2: TcxTreeListColumn;
cxTreeList1Column3: TcxTreeListColumn;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
CDS1RootKey: TStringField; // String[1]
CDS1CheckedChildKeys: TStringField; // String[20]
CDS1RootNodeChecked: TBooleanField;
btnClear: TButton;
procedure btnClearClick(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure SaveTreeState;
procedure ClearChecks;
procedure LoadTreeState;
end;

[...]
procedure TForm1.ClearChecks;
var
i,
j : Integer;
begin
for i := 0 to cxTreeList1.Count - 1 do begin
cxTreeList1.Items[i].Checked := False;
for j := 0 to cxTreeList1.Items[i].Count - 1 do begin
cxTreeList1.Items[i].Items[j].Checked := False;
end;
end;
end;

procedure TForm1.SaveTreeState;
var
i,
j : Integer;
RootNode,
ChildNode : TcxTreeListNode;
RootKey,
ChildKeys : String;
begin
CDS1.DisableControls;
try
CDS1.EmptyDataSet;

for i := 0 to cxTreeList1.Count - 1 do begin
RootNode := cxTreeList1.Items[i];
RootKey := RootNode.Values[1];
ChildKeys := '';
for j := 0 to RootNode.Count - 1 do begin
ChildNode := RootNode.Items[j];
if ChildNode.Checked then begin
if ChildKeys <> '' then
ChildKeys := ChildKeys + ',';
ChildKeys := ChildKeys + ChildNode.Values[2];
end;
end;
CDS1.InsertRecord([RootKey, ChildKeys, RootNode.Checked]);
end;
finally
CDS1.EnableControls;
end;
end;

procedure TForm1.LoadTreeState;
var
RootKey,
ChildKey : String;
ChildKeys : TStringList;
i : Integer;
RootNode,
ChildNode : TcxTreeListNode;

function FindRootNodeByKey(const Key : String) : TcxTreeListNode;
var
i : Integer;
begin
for i := 0 to cxTreeList1.Count - 1 do begin
Result := cxTreeList1.Items[i];
if CompareText(Result.Values[1], Key) = 0 then
Exit;
end;
Result := Nil;
end;

function FindChildNodeByKey(ParentNode : TcxTreeListNode; const Key : String) : TcxTreeListNode;
var
i : Integer;
begin
for i := 0 to ParentNode.Count - 1 do begin
Result := ParentNode.Items[i];
if CompareText(Result.Values[2], Key) = 0 then
Exit;
end;
Result := Nil;
end;

begin
cxTreeList1.BeginUpdate; // prevent treelist from updating while we load its state
ClearChecks;
try
// ChildKeys is a TStringList that we'll use to parse the list of child keys into individual values
ChildKeys := TStringList.Create;
try
CDS1.DisableControls;
try
CDS1.First;
while not CDS1.Eof do begin
RootKey := CDS1RootKey.AsString;
RootNode := FindRootNodeByKey(RootKey);
Assert(RootNode <> Nil); // check that we found the root node
RootNode.Checked := CDS1RootNodeChecked.AsBoolean;
ChildKeys.CommaText := Trim(CDS1CheckedChildKeys.AsString); // Loading ChildKeys
// by this assignment is what causes it to be parsed into individual keys
for i := 0 to ChildKeys.Count - 1 do begin
ChildKey := ChildKeys[i];
ChildNode := FindChildNodeByKey(RootNode, ChildKeys[i]);
Assert(ChildNode <> Nil);
ChildNode.Checked := True;
end;
CDS1.Next;
end;
finally
CDS1.EnableControls;
end;
finally
ChildKeys.Free;
end;
finally
cxTreeList1.EndUpdate;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
CDS1.CreateDataSet;
ClearChecks;
end;

procedure TForm1.btnClearClick(Sender: TObject);
begin
ClearChecks;
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
LoadTreeState;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
SaveTreeState;
end;

end.

关于Delphi cxtreelist循环遍历节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58991592/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com