未经验证
unit uCGoodsClass;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,ComCtrls,DBClient,DB,UnDBSCKT;
type
TuCGoodsClass=class(TObject)
private
public
function GetAllTreeNode():OleVariant;
function ModYData(item:string;id,parent_id:Integer):Integer;
function ADDData(item_name:string;parent_id:integer):integer;
function DelData(id:integer):integer;
procedure ShowAllTree(MyTree:TTreeview);
end;
implementation
function TuCGoodsClass.GetAllTreeNode():OleVariant;
var
DBopreator:TDBSCKT;
StrSql:string;
Res_Data:OleVariant;
begin
Res_Data:=Null;
DBopreator:=TDBSCKT.Create(nil);
strsql:=' 1=1 order by id asc';
Res_Data:=DBopreator.GetQuery('Goods_Class',StrSql);
FreeAndNil(DBopreator);
Result:=Res_Data;
end;
procedure CreateClassTree(MYCds:TClientDataSet;parent_id:Integer;root:TTreeNode;MyTree:TTreeView);
type
PTreeClassNode = ^TTreeClassNode; //用于存储每个树形节点的当前ID号和它的父的ID号。
TTreeClassNode = record
Cur_id :integer; //当前ID
Parent_id :Integer; //父类的ID
end;
var
ChildNode:TTreeNode;
Add_Node_Str: string;
PCur_Class_Node: PTreeClassNode;
i,next_parent_id:integer;
begin
if MYCds.Active then
begin
MYCds.Filtered:=False;
MYCds.Filter:='parent_id='+inttostr(parent_id);
MYCds.Filtered:=True;
if MYCds.RecordCount>0 then
for i:=0 to MYcds.RecordCount-1 do
begin
Add_Node_Str:=Trim(MYCds.FieldByName('item_name').AsString);
ChildNode:=MyTree.Items.AddChild(root,Add_Node_Str);
New(PCur_Class_Node);
PCur_Class_Node.Cur_id:=MYCds.FieldByName('id').AsInteger;
PCur_Class_Node.Parent_id:=MYCds.FieldByName('parent_id').AsInteger;
ChildNode.Data:=PCur_Class_Node;
MYCds.Next;
end;
if root<>nil then
begin
ChildNode:=root.GetNext;
if ChildNode <> nil then
begin
next_parent_id:=PTreeClassNode(ChildNode.Data).Cur_id;
CreateClassTree(MYCds,next_parent_id,ChildNode,MyTree);
end;
end;
end;
end;
procedure TuCGoodsClass.ShowAllTree(MyTree:TTreeView);
var
Cds :TClientDataSet;
Root:TTreeNode;
begin
cds:=TClientDataSet.Create(nil);
CDS.Name:='tempcds';
CDS.FetchOnDemand:=False;
CDS.PacketRecords:=-1;
Cds.Data:=GetAllTreeNode();
if not cds.IsEmpty then
begin
Root:=MyTree.Items.GetFirstNode;
CreateClassTree(Cds,0,Root,MyTree);
end;
FreeAndNil(Cds);
end;
function TuCGoodsClass.ModyData(item:string;id,parent_id:Integer):Integer;
var
DBopreator:TDBSCKT;
Res_Flage:Integer;
begin
//Res_Flage:=0;
DBopreator:=TDBSCKT.Create(nil);
Res_Flage:=DBopreator.UpdateSingleData('Goods_class',['item_name','parent_id'],[item,parent_id],' id='+inttostr(id));
FreeAndNil(DBopreator);
Result:=Res_Flage;
end;
function TuCGoodsClass.DelData(id:integer):Integer;
var
DBopreator:TDBSCKT;
Res_Flage:Integer;
begin
//Res_Flage:=0;
DBopreator:=TDBSCKT.Create(nil);
Res_Flage:=DBopreator.DeleteSingleData('Goods_class','id='+inttostr(Id));
FreeAndNil(DBopreator);
Result:=Res_Flage;
end;
function TuCGoodsClass.AddData(item_name:string;parent_id:Integer):integer;
var
DBopreator:TDBSCKT;
Res_Flage:Integer;
begin
//Res_Flage:=0;
DBopreator:=TDBSCKT.Create(nil);
Res_Flage:=DBopreator.InsertSingleData('Goods_Class',['item_name','parent_id'],[item_name,parent_id],True);
FreeAndNil(DBopreator);
Result:=Res_Flage;
end;
end.