What I’m trying to do:
I am a dancer with a voluntary dance group. We currently use a Google sheet to track members availability for events but I this has a number of disadvantages so I am trying to produce an Anvil App to better handle the job.
I have created three tables called ‘Users’ (actually the default users table with some extra fields), ‘Events’ and ‘UserEventYNs’. Each UserEventYN has a link to 1 event, a link to 1 User and YN column (text but could be boolean) indicating whether or not the user is available for the event. Each Event is linked from many YNs; one for each User. Each User is linked from many YNs; one for each Event Administrative parts of the UI will ensure that the three tables are maintained ‘in sync’. That’s the easy bit.
The main part of the UI, visible to each logged on user, is the bit that is challenging me. I would like to see the events listed in date order and the obvious answer is a data grid. Then within each row I would like to see the users and their YNs for that event which also seems to call for a data grid or at least a repeating panel but I cannot figure out how to achieve this nesting.
What I’ve tried and what’s not working:
On the ‘frmMain’ form I have a data grid which shows the event title and date for each event. I don’t have a clue how to proceed from there.
# this is a formatted code snippet.
# paste your code between ```
share a copy of your app
- Locate the grid’s Template Form, in the outline under Client Code.
- Right-click that form
- In the resulting pop-up menu, choose “Edit in standalone tab”
In that form’s “standalone” browser tab, you can add its own Data Grid.
That makes sense but my problem is really with the code to initialise the new datagrid. I want the grid to contain the ‘YN’ value of any ‘UserEventYN’ row which is linked to the ‘Event’ item of the outer grid AND the name (‘Forename’ and ‘Surname’) of the ‘User’ row which is linked to that ‘UserEventYN’ row.
You’ll have to do some data reshaping here.
Basically you to pass to each ‘row’ in your datagrid all the information it needs to do the job you’ve assignded it (to show all the user’s attending a given event, if I understand correctly).
I’d probably return the entire contents of the
UserEventYNs table to the client, and the restructure your data there.
We want to end up with a list of data elements that represents each event and all attendees to that event. I use a dictionary below but a tuple might be more common. For example:
# In frmMainSheet
rsvps = anvil.server.call('some_function_that_gets_usereventyns')
# Get our events
all_events = set([rsvp['EventLink'] for rsvp in rspvs]
# Create a collection that associates all my events with their attendees.
# Note I'm using rsvps again.
event_attendees = [
"rsvps": [rsvp for rsvp in rsvps if rsvp['EventLink'] == event]
for event in all_events
self.repeating_panel_1 = event_attendees
# In your repeating panel row:
self.event = properties['item']['event']
self.repeating_panel_rsvps.items = self.rsvps # This populates your nested datagrid
# In your nested repeating panel, properties['item'] will be a row from your RSVP table, so
# you can get the user and their response.
How does this put the users into the inner repeating panel? I don’t even see the users mentioned.
With 2 events and 3 users the UI should be something like this :
| Event 1 details ________________________ |
| | ||
| | User A A1YN ||
| | ||
| | User B B1YN ||
| | ||
| | User C C1YN ||
| Event 2 details ________________________ |
| | ||
| | User A A2YN ||
| | ||
| | User B B2YN ||
| | ||
| | User C C2YN ||
The key line is here:
We are passing this list to
repeating_panel.items. So, each ‘row’ in our repeating panel is getting an
item that is a dictionary with two keys:
event (the event row), and
rsvps (a list of rsvps, whjch include the user who rsvp’ed). So you can then set the nested
repeating_panel.items = self.item['rsvps'] or
repeating_panel.items = properties['item']['rsvps'].
That’s how you get the list of users into your nested repeating panel and was the key objecitve in reshaping the data in the first place.
Perhaps my problem is that I’m not sure what the server function should be that produces the rsvps.
Thanks Dan. What you provided did the trick. I’m still getting my head round linked tables being more used to SQL myself. Once I got it though the Anvil Python way is actually conceptually simpler. Thanks for your help and for being patient with a beginner.