Data grid and one to one linked table

Hello,

I’ve searched the forum and can’t find any answer.

I want to display a list of employees and their departments (employee table linked to departement table). The linked column in data grid shows as <LiveObject: anvil.tables.Row> instead name of the department.

Table Employee
Name, Department

Table Department
Name, Description

My data grid source is this function:
@anvil.server.callable
def get_employees():
return app_tables.employee.search()

I’m sure there is an easy way to do this, but I can’t find it,

Thanks for your help :blush:

Here is a clone to demonstrate binding to a linked row:
https://anvil.works/build#clone:XCB3F3STYHWDNAIG=VCEYG2RDY7W53EFXQHG3GB4B

If I understand correctly, you have a DataTable set up similar to this:

Now, suppose on your form you have a DataGrid component with a repeating panel inside of it (this is a common set up). If you want to display the employees and their associated departments, you can set up the following data binding in the IDE (see @stefano.menci’s answer below to see how to do this in the code editor instead of the IDE):

I know that is a bit hard to see so here is the code that I’m using to do the binding:

self.item['dept_link']['dept_name']
3 Likes

Somewhere, perhaps in the data binding of the label that shows the department, you have something like self.item['department']. Try changing that to self.item['department']['name'].

But be careful: this will work with few employees, but if the number of lines increases you will have one round trip per line. It’s always a good idea to gather all the data on the server side in one round trip and return it at once.

4 Likes

How much is that number of line (hundreds or thousands)? And how would i gather all the data on the server side? I guess that would be in my get_employees function?

I would say in the hundreds based on my experience but it may depend on the data itself and other factors.

In general, I usually pack up all the data I need into a list of dictionaries (including linked data if I need it), and return that to the client.

You can find more details on the forum by searching for optimizing performance. Here’s a bunch.

2 Likes

Much thanks to both of you @alcampopiano and @stefano.menci :slight_smile:

2 Likes