Consume Menu of the sql server in a Treeview Delphi

0

I have a menu in a sql server database where the Pid field is the id of the corresponding parent, at this time I will not have no grandchildren in the menu but for a future if I have them, the goal is to show that menu in tree form in a TreeView I am using delphi XE4, the elements of the table where the Pid is null is because they are parent nodes, this is my table in the Database, but I do not have the slightest idea of how to do it, I could someone to help me, Thanks in advance

    
asked by Raidel Fonseca 22.01.2018 в 20:03
source

2 answers

0

In my blog there are a couple of articles, although they are old you can serve: Convert a menu into a Treeview Create an application menu from text

The second shows you how to generate a Menu from a text file. It is not exact, but with few modifications you can adapt it to remove the data from the file, remove it from the SQL Server table.

The second shows how to convert the menu into a TreeView.

Both are with code that you can download, try and adapt to what you need.

    
answered by 01.02.2018 в 08:55
0

I enclose the code of an example that I have created from your question, it is entirely based on the information shown in the table. However, the method that I publish can be useful for any other table structure that stores a menu, it is made recursively for any level of menus.

It is important to emphasize that the order of the elements to be embedded in the TTreeView depends directly on the results of the query to the table containing the commands. That is, the tree elements are created in the order that the records of the query come. A parameter of type TDataSet is used so that it can be used with any database. And a Tobject class in each element of the tree to be able to locate the command that should be executed when double clicking (in this example) on the elements of the resulting TreeView.

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
    {Para este ejemplo se usan filtros, sin embargo, dependiendo
     del tipo de base de datos se pueden utilizar otros mecanismos,
     por ejemplo, modificar la clausula WHERE de la consulta}
    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;

    
answered by 17.01.2019 в 15:58