Cannot figure how to populate RepeatingPanel with my list

Hi Guys, I am trying really simple example where in init phase I have following:

# Any code you write here will run when the form opens.
self.repeating_panel_1.item_template = 'ItemTemplate2'
test = ['test', 'test1', 'test2']
self.repeating_panel_1.items = test
print self.repeating_panel_1.items

RepeatingPanel itself contains text component and there are no data bindings (I try to get to point where I get selected rows from Google Sheet loaded, but so far I cannot load already pre-created simple list ‘test’.

What’s interesting, I can see same number of text_boxes within repeating panel as is number of elements in my list, but I am not able to actually show those values ‘test’, ‘test1’, ‘test2’ (only 3 empty rows within RepeatingPanel).

I tried to compare to ToDo example app, but besides Data Bindings (which I don’t use), I am not able to understand what’s the difference.

Hello, and welcome to the forum.

Here is a simple example you can clone to your IDE by clicking on the link :

https://anvil.works/ide#clone:Q3INSMMJPHR4324A=JB3C3QE3ZMDTWTNGVMUBXIOA

See if you can work it out from this, if not post back here and I’ll see if I can help more.

Key points are :

  • Form1 sets an array of “mydata = value” pairs
  • I have bound the label in the ItemTemplate form to items[‘mydata’]

Anvil will auto-create a new ItemTemplate instance for each row of data in the items variable, and the data binding will ensure the label shows that data.

Does that help?

Wow david.wylie, thanks, that helped a lot, so my mistakes were:

  • not using ‘self.’ with my list
  • not using ‘header’ for each element of the list (via suggested dictionary syntax), that I could actually use in data binding
  • missing data binding

Thanks a ton, I was trying to figure this out for two days now :sunny:

1 Like

Pleasure to help.

Just some extra points - whilst data binding in the IDE is convenient, you can also do it in code inside the itemtemplate form if you need to do more complex stuff like formatting or manipulation of the data. You could do this (in ItemTemplate1) remembering to remove the data binding in the IDE first :

tmp = self.items['mydata']
self.label_1.text = tmp
if tmp == "HELP":
    self.label_1.background = "red"

The test variable you used doesn’t have to be part of the form class (ie using “self”), so that wasn’t wrong. I’m sure it’s possible to pass just an array of values, but it would have required some code in the template form to display it i think, and I find it much more readable to explicitly specify the field:data pairings, especially once you get into multi-column itemtemplate forms.

Hi David, very useful comment as well!

I tried to iterate on your example and use google spreadsheet and this works for me:

self.ws = app_files.ao_craft_sheet[0]
#self.repeating_panel_1.items = self.ws.rows
l = []
for r in self.ws.rows:
  l.append({'mydata': r['material']})

My question is if there is some more comfortable way how to get spreadsheet data into ‘data-binding-friendly’ format to display in repeating panel?

Unfortunately I’ve never used the google docs capabilities of Anvil, I’m afraid, so I can’t help with that. Sorry.

Someone else will hopefully jump in with help on that,

1 Like

Nevermind, you already made me progress hugely! :dash:

Though I have to say, I can’t see anything particularly wrong with that. Is it causing a problem?

No, no. It’s working, I was just wondering is simpler approach is possible (avoid ‘for’ cycle for example). I know that data tables have app_data.file.search() which can be used to populate the repeating panel list, maybe there’s equivalent for google docs api.