Jump to content

how to hide tunitreemenu items dynamically


Wilton Ergon

Recommended Posts

Hi,

 

As an approximate solution for now :)

procedure TMainForm.UniButton1Click(Sender: TObject);
var
  I: Integer;
  _llist: string;
begin
  with UniTreeMenu1 do
  begin
    SourceMenu.Items[0].Visible := False;

    _llist := '';
    for I := 0 to SourceMenu.Items.Count-1 do
      if not SourceMenu.Items[I].Visible then
        if _llist='' then _llist := '"'+SourceMenu.Items[I].Caption+'"'
        else _llist := _llist + ',"' + SourceMenu.Items[I].Caption+'"';

    if _llist<>'' then
    begin
      JSInterface.JSCall('getStore().clearFilter', []);
      JSInterface.JSCode(#1'.getStore().filterBy(function (record){ if (['+ _llist +'].indexOf(record.get("text"))>-1) return false; else return true;});');
    end
    else 
      JSInterface.JSCall('getStore().clearFilter', []);
  end;
end;

Best regards,

Link to comment
Share on other sites

  • 4 weeks later...

Hi,

 

As an approximate solution for now :)

procedure TMainForm.UniButton1Click(Sender: TObject);
var
  I: Integer;
  _llist: string;
begin
  with UniTreeMenu1 do
  begin
    SourceMenu.Items[0].Visible := False;

    _llist := '';
    for I := 0 to SourceMenu.Items.Count-1 do
      if not SourceMenu.Items[I].Visible then
        if _llist='' then _llist := '"'+SourceMenu.Items[I].Caption+'"'
        else _llist := _llist + ',"' + SourceMenu.Items[I].Caption+'"';

    if _llist<>'' then
    begin
      JSInterface.JSCall('getStore().clearFilter', []);
      JSInterface.JSCode(#1'.getStore().filterBy(function (record){ if (['+ _llist +'].indexOf(record.get("text"))>-1) return false; else return true;});');
    end
    else 
      JSInterface.JSCall('getStore().clearFilter', []);
  end;
end;

Best regards,

 

 

Great.

 

It only works when you are using TUniMenuItems as a SourceMenu? I'm creating the content of the menu at runtime and I can't make it work,

Link to comment
Share on other sites

  • 8 months later...
27 minutes ago, CoderU said:

How hide subitems?

Sample Tablet Application   has menu 

Dashboard -> Save

Dashboard -> Export

Dashboard -> Print

I mean runtime

How hide Save menu  or how hide all function if all function hide then hide parent menu Dashboard

Hello,

Ok I will check

Link to comment
Share on other sites

  • 10 months later...

Sorry but I also have problems using your solutions. 

I have the following uniTreeMenu structure: 

image.png.1cda29dcccf4adb5be31dcc92cf1f98e.png

.. and want to hide menu items "Management" and its children for "simple" users.

Therefor I am using the following code:

procedure TuDashboardFrame.UniPanelLoginClick(Sender: TObject);
var
  s : string;
  i : integer;
begin
  with MainForm do
  begin
    for i := 0 to UniTreeMenu1.Items.Count-1 do
    begin
     s := UniTreeMenu1.Items[i].AttachedMenuItem.Caption;
     if pos(s, 'Database Management Customers')>0  then
       UniTreeMenu1.Items[i].AttachedMenuItem.Visible := false;
    end;
    UpdateTreeMenu(UniTreeMenu1);
  end;
end;

in combination with your slightly adapted solution:

procedure UpdateTreeMenu (myMenu : TUniTreeMenu);
var
  I: Integer;
  _llist: string;
begin
  with myMenu do
  begin
    SourceMenu.Items[0].Visible := False;
    _llist := '';
    for I := 0 to SourceMenu.Items.Count-1 do
      if not SourceMenu.Items[I].Visible then
        if _llist='' then _llist := '"'+SourceMenu.Items[I].Caption+'"'
        else _llist := _llist + ',"' + SourceMenu.Items[I].Caption+'"';
    if _llist<>'' then
    begin
      JSInterface.JSCall('getStore().clearFilter', []);
      JSInterface.JSCode(#1'.getStore().filterBy(function (record){ if (['+ _llist +'].indexOf(record.get("text"))>-1) return false; else return true;});');
    end
    else
      JSInterface.JSCall('getStore().clearFilter', []);
  end;
end;

Unfortunately this is not working :(

Link to comment
Share on other sites

Thank you Sherzod!

I slightly modified the solution you sent me, which I also want to share with the community.

procedure HideMenueItems(var MyTreeMenu: TUniTreeMenu; const sHide: string);
var
  s,sH : string;
  i : integer;
  llist: string;
begin
  llist := '';
  sH := ','+sHide+',';
  for i := 0 to MyTreeMenu.Items.Count-1 do
  begin
    s := ','+MyTreeMenu.Items[i].AttachedMenuItem.Caption+',';
    if pos(s, sH)>0  then
    begin
      MyTreeMenu.Items[i].AttachedMenuItem.Visible := false;
      if llist='' then llist := '"'+MyTreeMenu.Items[I].AttachedMenuItem.Caption+'"'
         else llist := llist + ',"' + MyTreeMenu.Items[I].AttachedMenuItem.Caption+'"';
    end;
  end;
  if llist<>'' then
  begin
    MyTreeMenu.JSInterface.JSAssign('_rtext', [MyTreeMenu.JSControl.JSArray(llist)]);
    MyTreeMenu.JSInterface.JSCode('var me='#1';'+
      'if(me._rtext) me.getStore().each(function(record) {'+
      '    if (me._rtext.indexOf(record.get("text")) > -1) {'+
      '        record.remove()'+
      '    }'+
      '});'
    );
  end;
end;

procedure TMainForm.UniButtonHideClick(Sender: TObject);
begin
  HideMenueItems(UniTreeMenu, 'Dashboard,Database,Management,Customers');
end;

 

  • Like 1
Link to comment
Share on other sites

  • 6 months later...
On 2/1/2020 at 3:34 PM, Kattes said:

Thank you Sherzod!

I slightly modified the solution you sent me, which I also want to share with the community.


procedure HideMenueItems(var MyTreeMenu: TUniTreeMenu; const sHide: string);
var
  s,sH : string;
  i : integer;
  llist: string;
begin
  llist := '';
  sH := ','+sHide+',';
  for i := 0 to MyTreeMenu.Items.Count-1 do
  begin
    s := ','+MyTreeMenu.Items[i].AttachedMenuItem.Caption+',';
    if pos(s, sH)>0  then
    begin
      MyTreeMenu.Items[i].AttachedMenuItem.Visible := false;
      if llist='' then llist := '"'+MyTreeMenu.Items[I].AttachedMenuItem.Caption+'"'
         else llist := llist + ',"' + MyTreeMenu.Items[I].AttachedMenuItem.Caption+'"';
    end;
  end;
  if llist<>'' then
  begin
    MyTreeMenu.JSInterface.JSAssign('_rtext', [MyTreeMenu.JSControl.JSArray(llist)]);
    MyTreeMenu.JSInterface.JSCode('var me='#1';'+
      'if(me._rtext) me.getStore().each(function(record) {'+
      '    if (me._rtext.indexOf(record.get("text")) > -1) {'+
      '        record.remove()'+
      '    }'+
      '});'
    );
  end;
end;

procedure TMainForm.UniButtonHideClick(Sender: TObject);
begin
  HideMenueItems(UniTreeMenu, 'Dashboard,Database,Management,Customers');
end;

 


your example works perfectly, but it only removes the menu items, as I do to reassemble them and return the ones that were removed.


if (me._rtext.indexOf(record.get("text")) > -1) {'+
      '        record.remove()'+ ????

the example of friend Sherzod, applied only one filter to the menu items, this one does and removes the item, I need to recompose the items to return to the original state.

 

Link to comment
Share on other sites

  • 5 months later...
10 minutes ago, emin said:

I added a test caseunitreemenutest.rar

Try this approach for now:

      ...
      if llist<>'' then
      begin
        //MyTreeMenu.JSInterface.JSAssign('_rtext', [MyTreeMenu.JSControl.JSArray(llist)]);
        MyTreeMenu.JSInterface.JSAssign('_rtext', [MyTreeMenu.JSInterface.JSStatement('[' + llist + ']')]);
        MyTreeMenu.JSInterface.JSCode('var me='#1';'+
          'if(me._rtext) me.getStore().each(function(record) {'+
          '    if (me._rtext.indexOf(record.get("text")) > -1) {'+
          '        record.remove()'+
          '    }'+
          '});'
        );
      end;
      ...

 

Link to comment
Share on other sites

Hello Sherzod

Memory leak warning disappeared when I used new script. Thanks your help, above and beyond your sincere help I think we need a documentation source to handle that kind of problem. To be honest it doesn't make sense for me why the memory leak has accured and why it's disappeared after replacing the script. I always try to understand logic instead of copy-paste script. But this time I couldn't

Many thanks again

  • Upvote 1
Link to comment
Share on other sites

  • 4 months later...

I went back to addressing this item on my system, I still didn't have a solution, I was searching I found a code would it be possible to set no nodes visible?

 

 if not AMenu.Items[I].Visible then
        begin
          var lJs := #1'.getStore().getNodeById("' + AMenu.Items[I].Id.ToString + '");';
          AMenu.JSInterface.JSCode(lJs);
        end;

 

Link to comment
Share on other sites

"Yes, it can be cleared before creation, and then hidden or displayed after creation"

I actually want a total new set of Menu Items (added at runtime when needed for each criteria), not a bloated Items List that we try to hide unwanted Items.

Sherzod, how can we ADD MenuTree Primary Items and ADD associated Child Items (not remove) at runtime ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...