Hi @tim
Let’s say we have a Person
entity and a ToDoEntity
entity and we want to be able to add a new ToDoEntity
instance when creating or editing a Person
instance. Here is an example how it implemented:
- Open you
PersonEdit
descriptor and add new instance
component for ToDoEntity
entity:
<data>
<instance id="personDc"
class="com.company.form.entity.Person">
<fetchPlan extends="_base"/>
<loader/>
</instance>
<instance id="todoEntityDc" class="com.company.form.entity.TodoEntity">
<fetchPlan extends="_base"/>
<loader id="todoEntityDl"/>
</instance>
</data>
- Add a form for the
ToDoEntity
instance:
<split orientation="horizontal" id="splitId">
<form id="form" dataContainer="personDc">
<column width="350px">
<textField id="nameField" property="name"/>
<textField id="ageField" property="age"/>
<textField id="cityField" property="city"/>
</column>
</form>
<vbox>
<button id="addTodoBtn" caption="Add TODO"/>
<form id="todoForm" dataContainer="todoEntityDc" caption="TODO" editable="false">
<column width="350px">
<textField id="task1Field" property="task1"/>
<textField id="task2Field" property="task2"/>
<textField id="task3Field" property="task3"/>
</column>
</form>
</vbox>
</split>
I use a special button with addTodoBtn
id for activating the form, by default it is not editable.
3. Open the PersonEdit
controller and add the code below:
@Autowired
private Metadata metadata;
@Autowired
private InstanceContainer<TodoEntity> todoEntityDc;
@Autowired
private DataContext dataContext;
@Autowired
private Form todoForm;
@Subscribe
public void onBeforeCommitChanges(BeforeCommitChangesEvent event) {
TodoEntity todoEntity = todoEntityDc.getItem();
dataContext.merge(todoEntity);
}
@Subscribe("addTodoBtn")
public void onAddTodoBtnClick(Button.ClickEvent event) {
TodoEntity todoEntity = metadata.create(TodoEntity.class);
todoEntityDc.setItem(todoEntity);
todoForm.setEditable(true);
}
We use ClickListener of addTodoBtn
to activate the form and create new instance of TodoEntity
and add this instance to InstanceContainer. Also we use listener of BeforeCommitChangesEvent to add the TodoEntity
to the DataContext.
Here is a result how it looks like:
Regards,
Nadezhda.