Problem with code created data grid

What I’m trying to do:
Trying to create a data grid from code as the number of columns may vary

What I’ve tried and what’s not working:
Create the datagrid with appropriate columns.
Create a list of dictionaries, ‘thedata’, to act as the source of the repeating panels items.
Create the repeating panel : self.therepeatingpanel = RepeatingPanel(item_template=DataRowPanel())
Add the RP to the grid : self.thegrid.add_component(self.therepeatingpanel)
Set the RP items : self.therepeatingpanel.items = self.thedata
Add the grid to the form : self.add_component(self.thegrid)

I now want to add components to the repeating panel’s template, as can be done very powerfully in the IDE, so that the look of my column entries can be customised. For example, I would like to ‘drop’ a label in the first column (id = 0, datakey = ‘eventdate’, title = ‘Event Date’) so that I can format the text of my eventdates to a font size of 12 and text which in the IDE I would produce by binding self.item[‘eventdate’].strftime(‘%d/%m/%y’). Unfortunately I have no idea of how to add the label. I presume I must define a label instance and then use .add_component but what do I add the label TO? Is this even possible?
Code Sample:

# this is a formatted code snippet.
# paste your code between ``` 

Clone link:
share a copy of your app

The key is in what the RepeatingPanel repeats. For each row, it creates an instance of a Form class. You supply the class, the RepeatingPanel repeats it.

See RepeatingPanels.

This is divide-and-conquer at work. It is up to your template-form class. Each instance should to create its own instance of your label – and any other component that you want on that row. Since your template form is taking that responsibility, your grid’s form doesn’t have to do it.

In the DataGridJson custom component I used two template forms, one with the DataRowPanel only and one with the DataRowPanel inside a Link. The former can contain labels to show the content of the dictionary values or links to show the same values and make them clickable. The latter makes the whole row clickable, unless some specific columns are clickable themselves.

You can clone it and look at how I added the components.

It was long time ago, I don’t remember how I did it. You can clone it, have a look at what I did, and see if you can find some inspiration. And let me know if you have some questions, maybe adding a clone link to your app.

1 Like

Hi Stefano

I think the DataGridJson component could be just what I wanted but I don’t know how to import it into my app. I have downloaded the clone so that it is in my list of Apps but don’t know how to proceed further.

Cheers, Nick

Here you can see how apps can depend on each other: Anvil Docs | Depending On Other Apps

Let me know if you still need help.

The RepeatingPanel is added with the code

self.therepeatingpanel = RepeatingPanel(item_template=DataRowPanel)
self.thegrid.add_component(self.therepeatingpanel)

For a DataGrid created in the IDe I could simply go to the repeating panels template form, and drop a component onto the first slot/column then bind it to the appropriate data item using self.item[‘dataitemname’]. This is what I want to achieve in code but how do I tell Anvil which column/slot of the template I want my label in?

See Manipulating Data Grids using code for some examples.