Table with One-to-Many Relation - Format Data Bindings & Multiple Rows Output

What I’m trying to do: Hey all, I have two tables > Employees & Agencies. Agencies has a few standard text columns and one column linking to the Employees table using the “Multiple Rows” linkage. I’ve added link field to the repeating panel and configured the data bindings, however, I have a couple of questions:

  1. On a repeating panel - I cannot seem to get a list of the linked employees to display properly - does anyone have generic code they can share?
  2. I would like to display the employees’ information on an alert popup when the user clicks on the respective employee link from the column - does anyone have generic code they can share?

Thank you for any and all assistance.

What are the issues you’re encountering?

You can pass any iterable to a repeating panel items property so you should be able to basically do:

self.repeating_panel_1.items = my_agency['employees_links']

For #2, you can pop up a form in an alert, so for this type of use-case I usually build a form that renders attributes you want to show the users, and takes a row as an argument. So you end up with something like:

my_form = EmployeeForm(self.item) # where self.item is an employee row
alert(my_form, large=True)

@danbolinson my bad, I should have been a bit more specific.

Issue No.: 1

In the data binding, if I use…

self.item['employees'][0]['last_name']

I’ll get a single name from the linked rows. However, if I use…

self.item['employees'][:]['last_name']

I get this error: list indices must be integers or slices, not str. I’ve used the list() constructor to no avail.

When I use this code…

str([emp['last_name'] for emp in self.item['employees']])[1:-1]

If produces a list like this: ‘last_name1’, ‘last_name2’, etc, etc

Issue No.: 2
I am populating a form dropdown from a table. My code is as follows:

self.input_employee_agency.items = [(x['acronym'], x) for x in anvil.server.call('fetch_record', 'agencies', 'acronym')]

I then have a simple add_row function as follows:

@anvil.server.callable
def add_record(table_name, record_row_dict):
  getattr(app_tables, table_name).add_row(**record_row_dict)

I can’t figure out how to bind the “selected_value” to the linked table column. I would do it manually with a stand alone function but there will be a ton of fields on the form and I wanted to avoid having to program each field in the function. I am receiving an error on submission.

I believe it will produce an object of type str, not of type list. The two might be interchangeable in TCL (see everything is a string), but this is Python, which has a stronger type system.

To get an actual list of individual strings, if that’s what you need, just pare it down to:

   [ emp['last_name']    for emp in self.item['employees'] ]
1 Like

@p.colbert thank you so much this worked perfectly. Any chance you know the answer to the second issue?

Where you’re getting the second issue, it isn’t clear to me what you’re trying to accomplish.

I recommend starting a new Forum Topic, on that specific subject. That way, when the answer becomes evident, it won’t be buried under an unrelated topic. It will be easier for all the forum members and visitors to find.

1 Like

sounds good, thank you, @p.colbert