In the documentation, repeating panel rows can access the repeating panel through their parent
attribute:
I am observing that the parent
is None
- what am I missing?
clone link:
https://anvil.works/build#clone:CIFZGQ4G7FHOHYUB=QILP34RPAZKIHBPDF4K6WSIM
You are checking self.parent
in the __init__
function. The parent is not set yet there, the earliest you can access self.parent
is in a form_show event handler.
Got it. Seems you are correct.
Is this a bug? I can’t find that in the documentation.
My use case: I often want to modify a dictionary in place and have each key represented as a row in a repeating panel. So having a shared state available through the repeating panel is helpful.
https://anvil.works/build#clone:PQXOA25G2BOKIXPC=VPUXWOV6XT4CYVY3NLLHWNN7
In general I think there are lots of cases where repeating panel rows should ‘know’ about the parent, so I’m surprised that doesn’t happen as part of init_components (or earlier), or passed as a property or something. Is there a work-around to force the setting of parent
earlier?
Not a bug. Imagine the code that creates each row template
for item in self.items:
row = template (item=item)
self.add_component(row)
The init method is executed before the row is added to the parent.
It could certainly be changed to pass the parent in to init, but that would be a feature request.
You can do shared state through client modules, which might be a workaround for you.
1 Like
Thanks, that example is helpful. Basically I think my question boils down to how to pass arguments and keyword arguments to a repeating panel, i.e.:
for item in self.items:
row = template (item=item, my_keyword_arg='something')
self.add_component(row)
I’m trying to avoid client modules here - it’s important to me that the data exists as an attribute of the calling form. For example if I call the same form twice they should each have their own ‘state’ vs sharing a state through an imported client module (there’s lots of possible workarounds here as well).
So I think you’re right that this is a feature request…
Hi ,
If you want the parent of a repeating panel you can to do a ‘reverse’ lookup through get_open_form() .
So if your parent is a datagrid called DataGrid1 then you can in your code in the repeating panel do something like:
parent=get_open_form().DataGrid1
prop1=parent.prop1
(there should of course be a self.prop1 in DataGrid1)
You can also pass arbitrary data in the items property of the repeating panel, so you can set repeating_panel1.items=[{‘col1’:1,‘col2’:2,‘prop1’:[1,2,3],{‘col1’:4,‘col2’:5,‘prop1’:[1,2,3]}] . Doesn’t throw an error if you’ve got only 2 colums, but is kind of wastefull if your prop1 is the same for all rows.