Navigation: one click handler for all Links

What I’m trying to do:
I’m trying to implement the navigation using the advance tip with the one click handling for all links
What I’ve tried and what’s not working:
When I add the code below. I"ve commented it out for now because it doesn’t show any left navigation menu with that code running. So if you uncomment that and run it it shows a blank. I’ve set my links to run the def navigation_link_click function.

So did I do something wrong or is there a bug?

# click handler for all menu links
#self.settings_left_menu_link.tag.form_to_open = private_settings_form()
#self.trade_settings_left_menu_link.tag.form_to_open = user_settings_form()

Clone link:
https://anvil.works/build#clone:642E5CROT7KHF3BZ=NJYZO25OTNNJPIX56N5Z5FC5
share a copy of your app

It works fine for me when those two lines are added to the bottom of the __init__ function, e.g.:

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    self.settings_left_menu_link.tag.form_to_open = private_settings_form()
    self.trade_settings_left_menu_link.tag.form_to_open = user_settings_form()

ok awesome, got it working and got the links to work too thank you.

maybe they need to provide instructions that it needs to be under the init()
since i don’t thikne i read that anywhere just looking at the image, it seems like it should be place wrongly below the import statements.

1 Like

Yeah, that is misleading. I added a link to the documentation suggestions, hopefully that’ll get fixed.

I guess along the same lines of this problem, I’m also trying to make it appear that its clicked.

my code isn’t applying the 'selected" attribute to the role:

self.settings_left_menu_link.role = 'selected'

is there a tool to troubleshoot why certain things aren’t going through? it sems like I followed the instruction right. I can change it manually but its not changing in code.

I just tried adding a print statement

 print(f"self.settings_left_menu_link.role = {self.settings_left_menu_link.role}")

but where can I see the output of that.

You put that code into a function that is never getting called.

Normally the console and the app logs would both show that output. But, as noted above, the function it’s in is never getting called, so the output never happens.

oh… I see what you are saying…


ok hmm… I’ll need to figure out how manage it all with the click handler.

so how do I get the click event and the code get that click event so I know which link got clicked and then set the role for it

hmm ok thinking for myself chime in anytime…
not trying to get free handouts but its helping get familiar with anvil since I’m new to it.

I can see inside the “form_to_open” object, but can’t see all the attributes of the form, but what I really need is the name or accessing the name of the form, so if i know what form is opened I can make the link look "selected.

Thanks for letting me post everyone, and talking out lout to myself. I figured it out, googled and found dir() helps to look whats inside the object and I can figure out the rest.

image

  def navigation_link_click(self, **event_args):
    form_to_open = event_args['sender'].tag.form_to_open
    self.content_panel.clear()
    self.content_panel.add_component(form_to_open)
    print(f"form_to_open = {form_to_open}")
    print(f"dir(form_to_open) = {dir(form_to_open)}")
    print(f"event_args = {event_args}")
    print(f"form_to_open.__name__ = {form_to_open.__name__}")
    if(False):
      pass
    elif(form_to_open.__name__ == 'private_settings_form'):
      self.reset_links()
      self.settings_left_menu_link.role = 'selected'
    elif(form_to_open.__name__ == 'user_settings_form'):
      self.reset_links()
      self.trade_settings_left_menu_link.role = 'selected'
      ```
1 Like

In that line, note that event_args['sender'] is the actual link that was clicked. So you should be able to do something like:

self.reset_links()
event_args['sender'].role = 'selected'

Phew… awesome thank much cleaner code… Where would I find information like that, or how do you know or where do you learn that 'event_args[‘sender’]" is the actual link clicked?

I’m pretty sure someone on the forum told me what event_args['sender'] was, when I had a similar question to yours.

This actually is in the documentation. Under Events see event_args.

1 Like