How Do I Add a New Child Node at Runtime? (Magic xpa 4.x)
When the user is working with a tree, much of the functionality is the same as working with a table. However, in a tree the data is not flat. So when the user wants to “add a line”, that line can either be a sibling node or a child node. In either case, it is up to you as the programmer to be sure the parent node and current node fields are initialized correctly when a new node is created.
-
Set up a button that raises the event Create Child, and label it “Create child” or whatever is appropriate to the application. “Create child” does not exist by default on the overhead menu, so if you want to make it a runtime option, you’ll need to add it.
-
Create two variables somewhere below the Parent and Node variables, in the Data View. These will be used in an Init to initialize those two fields if a new record is created.
Note: Adding the variables above the Parent and Node variables will cause recompute and affect the current node.
-
Create a handler for the Create Child event, with Propagate set to Yes.
-
Within the handler, update the initialization fields appropriately so the new record will be a child. The Parent field should have the value of the current field, which can be automatically obtained by using the function TreeValue(0).
You also need to initialize the Node field with a unique value. In our example, we used a function we wrote that will automatically fetch a unique key for us.
Now, the new node will be initialized appropriately, and the user can fill in the rest of the data as desired.
The Online and Rich Client Samples projects (program TR05, TR20, RTR05 and RTR20)