How to access multi-level nested items?

Hello. I have a form with a few Repeating panel, nested inside each other. The bottom level items include disabled checkboxes. Also, there is one checkbox on the top level. And if it is checked, the bottom level checkboxes should become enabled. But I have no idea, how to get access to the bottom level elements from the function on top level form. Here is screenshot:

Hi @ivan.b and welcome to the forum,

The top level checkbox will need to somehow know about/know how to access the child checkboxes…

There are choices about design here and how you manage the hierarchy - it may depend on how nested these components are. And to which forms they belong.

raise_event_on_children may be a useful tool (with a custom event)
https://anvil.works/docs/api/anvil#RepeatingPanel_methods

But it only goes one level deep so will need to be called by the repeating panel to which the bottom level checkboxes have as a parent. This means that the top level checkbox now needs to know about/know how to access the repeating panel. This may be easy depending on the hierarchy of components.

You might also consider linking the checkboxes in a different way. Perhaps the top level checkbox could have tag.children as a list - and when these sub checkboxes are added to the screen they append themselves to the list. Now the sublevel checkboxes need access to the top level checkbox. If the top level checkbox is on the open_form you could take advantage of get_open_form() like get_open_form().top_checkbox

Or maybe you outsource this behaviour to its own module and then the modules job is to know about the top level checkbox and the nested checkboxes. When sublevel checkboxes are added to the screen they reach out to the module to let it know they exist. When the top level checkbox changes it reaches out to the module to tell those sub level checkboxes it knows about to also change…

1 Like

Thanks for the answer. But still, i don’t see how module can tell sub level checkboxes to change their property?

sublevel = [] # list of checkboxes

def add_sublevel(cb):
  global sublevel
  sublevel.append(cb)

def set_sublevel_checked(checked):
  for cb in sublevel:
    cb.checked = checked

Something like that maybe where you import the module into the relevant forms and call the relevant methods.

You might also want to have a look at my event propogation module:

2 Likes