1-n relation in a single screen

Good morning,
I am now starting to use CUBA Studio to make some small projects, so I apologize in advance for the probably trivial question.
I have a main Entity “entA” and an “entB” entity that refers to “entA” with a 1-n relationship (thus, the primary key of “entA” is a foreign key of “entB”).

I would like to create an entry / modification screen for “entA” in which:

  1. In case of new entry, only the “entA” fields are displayed to the user;

  2. In case of modification of an existing record, the “entA” fields and a grid containing the “entB” records linked to the specific “entA” record are displayed to the user, with the possibility of adding / removing “entB” (in case of addition, the “entA” key should already be filled in the form).

Is it possible to do this?

How can I pass the key of “entA” and filter the grid of “entB”?

Thank you

Hi,
It is fully possible.

  1. In the editor screen of the “entA” you should add a “collection” data container to the screen, with the type of entB.

  2. Add table or grid component to the screen, connect it to the entB’s collection container.

  3. Assign “id” attribute to the grid. Or better, wrap grid into groupBox or vbox container, assign id to it.

  4. Inject components to the editor’s screen controller.

  5. Subscribe to the “InitEntityEvent” event in the screen controller. It is fired only if the entity being edited is “new”, not saved to the database yet.
    Hide necessary components on this event handler by invoking .setVisible(false).

Use dataLoadCoordinator and query filter in the collection container for that.

See here:
https://doc.cuba-platform.com/manual-7.2/gui_DataLoadCoordinator.html

select e from demo_Pet e where e.owner = :container_ownersDc

1 Like