How to dynamically create multiple objects and their associations in one page based on user input

2
Hi, I am struggling to figure out how create the following logic in my application. The application is a form. In the domain model are two entities with a 1-* relationship; Calculation(1) to Child(*). These are both non-persistable entities. In one page on the form, the user is asked to input the first name of each of their children. They can input the name of only one child, or press a + button to show another input field so they can add the name of another child and so on. When, the user clicks next, or as each text box is changed I would like the application to create an object for each child, so if the user enters 4 children's names, then 4 objects of the entity 'Child' would be created, and the 'Name' attribute populated with the names the user entered. I would like these objects to have an association with the current session's 'Calculation' object which is storing the rest of the information the user has input so far. I basically have no idea what needs to be on this page, i.e. which data container widgets I should use to house the input fields, and how to commit the child objects and set their associations. Apologies for such a non-specific question, as you can tell I'm new to this, but have spent ages going through the training material and forum and just can't figure it our, so any help would be greatly appreciated.
asked
2 answers
2

Hi Bryn,

Some thoughts:

  • First, you'll need to create the Child objects before the user can input child names, as Mendix needs an instance of an object to input data on a page
  • Because both of these entities are non-persistable, you'll need to access the Children objects via association (NPEs can only be retrieved via Association or Microflow on a page, not Database or Xpath)
  • On your page, I would put a Dataview with the Calculation object.  Depending how you will open this page, you can get your current Calculation object in this Dataview either via microflow or context.  You'll use context if you are opening this page from another page or a microflow from which you can pass a Calculation object, otherwise, use Microflow source and build a microflow to return the Calculation object attached to your current session.
  • Inside of your Dataview, put an editable Listview that retrieves the Child objects via association.  In this Listview put a textbox for the Child's name
  • Finally, create another microflow that takes a Calculation object as a parameter, creates a Child object and sets the association between the Child object and the Calculation object.  So that you can see how it works, place an Action button in your Dataview that calls this microflow.  Each time you push this button, you should get a new Child object on the page.  No need to commit any of these objects as these are NPEs so they are not stored in the database
  • Once you get this working, you can play around with how to trigger creation of a new child (onChange of the Child Name text box, use the action button your created, or some other way of creating)

Hope that helps,

Mike

answered
1

When creating the child object with the + button you can parse the parent object in the microflow, in the create action of the child object you should set the association to the parsed parent object.

Make sure the association points from child to parent (one calculation object possible has n- childs)

The form should contain:

- Calculation object

- List view (or similar) with the child objects (via association)

The + button should create a new child object

The name input field can be linked to the name attribute on the child attribute in the list view

 

answered