How Do I Add a New Sibling 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 Line, and label it “Create sibling” or whatever is appropriate to the application. The user can also press F4 or select Edit->Create Line, which will add a sibling node, but that might not be obvious to the user.
-
Create two variables somewhere above 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.
-
Create a handler for the Create Line event, with Propagate set to Yes.
-
Within the handler, update the initialization fields appropriately so the new record will be a sibling. The Parent field should have the value of the parent, which can be automatically obtained by using the function TreeValue(1).
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 TR06 and RTR06)