Nested grids in code

I am trying to nest a data grid inside another data grid in code.

In the form’s init I create the first (outer) data grid and define its columns. I the create a repeating panel, set its items property and add it to the outer grid.
Next I create the second (inner grid and define its columns.
How do I add this grid in the second column of the first?

Code Sample:

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    thelist = anvil.server.call('createdatastructure')
    outergrid = DataGrid()
    outergrid.columns = [
      {'id':'A', 'title':'Event', 'data_key':'Event', 'width':200},
      {'id': 'B', 'title':'Y/Ns', 'data_key':'YN', 'width':200},
    ]
    self.add_component(outergrid)
    outergridrepeatingpanel = RepeatingPanel(item_template=DataRowPanel)
    outergridrepeatingpanel.items = thelist
    outergrid.add_component(outergridrepeatingpanel)
    innergrid = DataGrid()
    innergrid.columns = [
      {'id':'C', 'title':'Name', 'data_key':'Name','width':'50'},
      {'id':'D', 'title':'Y/N', 'data_key':'YN', 'width':'50'},
    ]
    self.init_components(**properties)

Clone link:
share a copy of your app

It’s a bit indirect, thanks to the way that a RepeatingPanel works.

Your RepeatingPanel, including the one for the outer grid, will be repeating
(creating instances of) some Form, once per data row that it receives. That Form is called a Row Template, and it is this Form that must contain the inner grid.

So, you have a nice division of responsibilities here: your outer form, with its grid, can be responsible for managing its grid, and that grid’s data; and likewise for the code of your Row Template form, with its own grid instance.

So, code up your Row Template form (class) to handle its part of the job. Divide and conquer.

3 Likes

That got the job done. Thanks as ever p.c. for your help.

I’d like to add that p.colbert is only one of several regular contributors who not only offer their help but are polite and tolerant towards ‘newbie’ questions. This helps to make working with Anvil a positive experience. Thanks to all of you.

5 Likes