- //采用递归方法,D7编译调试通过。
- //数据采用ADOQuery读取,并将数据暂存在一个动态数组中,树形列表控件为TreeView。
- procedure TForm1.LoadTreeInfo;
- type
- TInfo = record
- ID, //代码
- Name, //名称
- SuperID //上级代码
- : string;
- //附加字段随需添加
- end;
- var
- sql: string;
- i, nCount: Integer;
- arrInfo: array of TInfo;
- NewNode: TTreeNode;
- //加载一个节点
- procedure InitOneNode(ANode: TTreeNode; AId: string);
- var
- k: Integer;
- begin
- for k := 0 to length(arrInfo) - 1 do
- if arrInfo[k].SuperID = AId then
- begin
- NewNode := TreeView1.Items.AddChild(ANode, arrInfo[k].Name);
- InitOneNode(NewNode, arrInfo[k].ID);
- end;
- end;
- begin
- TreeView1.Items.BeginUpdate;
- TreeView1.Items.Clear;
- sql := 'select ID, Name, SuperID from DictionaryTable order by ID';
- ADOQuery1.Close;
- ADOQuery1.SQL.Text := sql;
- ADOQuery1.Open;
- nCount := ADOQuery1.RecordCount;
- if nCount > 0 then
- begin
- SetLength(arrInfo, nCount);
- for i := 0 to nCount - 1 do
- with arrInfo[i] do
- begin
- ID := Trim(ADOQuery1.FieldByName('ID').AsString);
- Name := Trim(ADOQuery1.FieldByName('Name').AsString);
- SuperID := Trim(ADOQuery1.FieldByName('SuperID').AsString); //无没有此字段,可根据上下级编码规则赋值
- ADOQuery1.Next;
- end;
- end;
- ADOQuery1.Close;
- if nCount > 0 then
- begin
- InitOneNode(nil, ''); //假设顶级代码为空白
- TreeView1.FullExpand;
- TreeView1.Items.EndUpdate;
- end;
- end;
递归加载树形列表的过程(函数)
最新推荐文章于 2023-08-21 15:00:56 发布