Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS subject;
- CREATE TABLE IF NOT EXISTS subject ( # Comment
- subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # Subject ID
- subject VARCHAR(25) NOT NULL, # Subject name
- parent_id INT UNSIGNED NULL DEFAULT NULL, # Parent ID as seen from
- PRIMARY KEY (subject_id), # the diagram refers to
- UNIQUE (subject), # the subject_id field
- INDEX (parent_id),
- CONSTRAINT fk_subject_parent
- FOREIGN KEY (parent_id)
- REFERENCES subject (subject_id)
- ON DELETE RESTRICT
- ON UPDATE CASCADE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- INSERT INTO subject (subject, parent_id) VALUES
- ('Science', NULL),
- ('Mathematics', 1),
- ('Calculus', 2),
- ('Algebra', 2),
- ('Geometry', 2),
- ('Languages', NULL),
- ('English', 6),
- ('Latin', 6);
- SELECT * FROM subject;
- ╔════════════╦═════════════╦═══════════╗
- ║ subject_id ║ subject ║ parent_id ║
- ╠════════════╬═════════════╬═══════════╣
- ║ 1 ║ Science ║ NULL ║
- ║ 2 ║ Mathematics ║ 1 ║
- ║ 3 ║ Calculus ║ 2 ║
- ║ 4 ║ Algebra ║ 2 ║
- ║ 5 ║ Geometry ║ 2 ║
- ║ 6 ║ Languages ║ NULL ║
- ║ 7 ║ English ║ 6 ║
- ║ 8 ║ Latin ║ 6 ║
- ╚════════════╩═════════════╩═══════════╝
- DELIMITER$$
- DROP PROCEDURE IF EXISTS get_parent_subject_list;
- CREATE PROCEDURE get_parent_subject_list ()
- BEGIN
- SELECT subject_id, subject
- FROM subject
- WHERE parent_id IS NULL
- ORDER BY subject ASC;
- END$$
- DROP PROCEDURE IF EXISTS get_child_subject_list;
- CREATE PROCEDURE get_child_subject_list (IN parentID INT)
- BEGIN
- SELECT subject_id, subject
- FROM subject
- WHERE parent_id = parentID
- ORDER BY subject ASC;
- END$$
- DELIMITER ;
- procedure TForm1.CreateSubjectTreeView(Sender: TObject);
- var
- i : integer;
- begin
- i := 0;
- q1.SQL.Clear;
- q1.SQL.Add('CALL get_parent_subject_list()');
- q1.Open;
- q1.First;
- while not q1.EOF do
- begin
- TreeView.Items.Add(nil, q1.Fields[1].Value);
- q2.SQL.Clear;
- q2.SQL.Add('CALL get_child_subject_list(' +
- VarToStr(q1.Fields[0].Value) + ')');
- q2.Open;
- q2.First;
- while not q2.EOF do
- begin
- TreeView.Items.AddChild(TreeView.Items.Item[i], q2.Fields[1].Value);
- q2.Next;
- end;
- i := TreeView.Items.Count;
- q1.Next;
- end;
- end;
- +- Science
- | |
- | +- Mathematics
- |
- +- Languages
- |
- +- English
- +- Latin
- +- Science
- | |
- | +- Mathematics
- | |
- | +- Calculus
- | +- Algebra
- | +- Geometry
- |
- +- Languages
- |
- +- English
- +- Latin
- Select ID, Title, This, That from TREE where Parent_ID = :ID
- var i,imax,ikey,iParent : integer;
- aNode,aParentNode : TTreeNode;
- aData : TMyData;
- aContainer : TSparseObjectArray; // cDataStructs , delphi fundamentals
- aNodeIndex : TSparseObjectArray; // delphi 7
- begin
- try
- aContainer := TSparseObjectArray.Create(true);
- aNodeIndex := TSparseObjectArray.Create(False);
- imax := 10000;
- // create test data;
- for i := 1 to imax do
- begin
- aData := TMyData.Create;
- aData.iKey := i;
- aData.iParent := Random(imax); // random parent
- aData.Data := 'I:' + IntToStr(aData.iKey);
- aContainer.Item[i] := aData;
- end;
- tv1.Items.Clear;
- tv1.Items.BeginUpdate;
- // build tree
- // First Pass - build root tree nodes and create cross ref. index
- for i := 1 to imax do
- begin
- aData := TMYData(aContainer.Item[i]);
- aNode := tv1.Items.AddChild(nil,aData.Data);
- aNodeIndex.Item[aData.iKey] := aNode;
- end;
- // Second Pass - find parent node using index and move node
- for i := 1 to imax do
- begin
- aData := TMYData(aContainer.Item[i]);
- aNode := TTreeNode(aNodeIndex.Item[aData.iKey]);
- if aNodeIndex.HasItem(aData.iparent)
- then begin
- aParentNode := TTreeNode(aNodeIndex.Item[aData.iparent]);
- aNode.MoveTo(aParentNode,naAddChild);
- end;
- end;
- tv1.Items.EndUpdate;
- tv1.Select( tv1.Items.GetFirstNode);
- finally
- aContainer.Free;
- aNodeIndex.free;
- end;
- end;
- type
- tElementoMenu = Class(TObject)
- Comando : String;
- //Nombre : String;
- ID : String;
- End;
- ...
- procedure TForm1.CrearMenuDeArbol(dsOrigen: TDataSource; CampoID, IDPadre,
- CampoComando, CampoCaption, CampoVisible: String; Raiz : TTreeNode = Nil);
- var
- RamaActual, PrimeraRama : TTreeNode;
- ElementoMenu : TElementoMenu;
- iIndiceImagen : Integer;
- begin
- RamaActual := Nil;
- PrimeraRama := Nil;
- if not assigned(Raiz) then
- VaciarArbol;
- with dsOrigen.DataSet do
- begin
- //For this example I use filter, however it can be use with WHERE sentence
- Filtered := False;
- IF not assigned(Raiz) then
- Filter := IdPadre + ' IS NULL '
- else
- Filter := IDPadre + ' = ' + TElementoMenu(Raiz.Data).ID;
- Filtered := True;
- First;
- while not Eof do
- begin
- if FieldByName(CampoVisible).AsString = 'Y' then
- begin
- ElementoMenu := TElementoMenu.Create;
- ElementoMenu.Comando := FieldByName(CampoComando).AsString;
- ElementoMenu.ID := FieldByName(CampoID).AsString;
- //ElementoMenu.Nombre := FieldByName(CampoName).AsString; //Otros datos para agregar al elemento del menu
- iIndiceImagen := 0;
- if Not Assigned(Raiz) then
- RamaActual := TreeView1.Items.AddObject(Nil, FieldByName(CampoCaption).AsString, ElementoMenu )
- else
- Begin
- RamaActual := TreeView1.Items.AddChildObject(Raiz, FieldByName(CampoCaption).AsString, ElementoMenu );
- iIndiceImagen := 1;
- End;
- RamaActual.ImageIndex := iIndiceImagen;
- RamaActual.SelectedIndex := iIndiceImagen;
- end;
- Next;
- end;
- if not Assigned(Raiz) then
- PrimeraRama := TreeView1.Items.GetFirstNode
- else
- PrimeraRama := Raiz.getFirstChild;
- while Assigned(PrimeraRama) do
- begin
- CrearMenuDeArbol(dsOrigen, CampoID, IDPadre, CampoComando, CampoCaption, CampoVisible, PrimeraRama);
- PrimeraRama := PrimeraRama.getNextSibling;
- end;
- end;
- end;
- procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- VaciarArbol;
- end;
- procedure TForm1.TreeView1DblClick(Sender: TObject);
- begin
- if Assigned(treeView1.Selected) then
- ShowMessage(TElementoMenu(treeView1.Selected.Data).Comando);
- end;
- procedure TForm1.VaciarArbol;
- var
- itm : TTreeNode;
- begin
- while TreeView1.Items.Count > 0 do
- begin
- itm := TreeView1.Items[TreeView1.Items.Count-1];
- TElementoMenu(itm.Data).Free;
- TreeView1.Items.Delete(itm);
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- CrearMenuDeArbol(ds1, 'ID', 'PID', 'NAME', 'CAPTION', 'ISVISIBLE');
- Treeview1.FullExpand;
- end;
Add Comment
Please, Sign In to add comment