Adding new node of another type when adding a node of other type in a group

Hi,

Inside a group I am dropping a particular type of node(type A) which I am adding but along with that I also want to add another node(type B) whenever node of type A is dropped inside a group.I am using
addNodeData(data); functionality of GoJS library and written a code like this inside ValidateObject function
like this.But it is not working .

var data=new Array();
data[“key”] = generateUUID();
data[“text”] = “Post”;
data[“stroke”] = “#888”;
data[“background”] = “#F7CA18”;
data[“color”] = “#364150”;
data[“category”] = “Default”;
data[“geometryString”] = “M13.942 6.039c0.038-0.174 0.058-0.354 0.058-0.539 0-1.381-1.119-2.5-2.5-2.5-0.222 0-0.438 0.029-0.643 0.084-0.387-1.209-1.52-2.084-2.857-2.084-1.365 0-2.516 0.911-2.88 2.159-0.355-0.103-0.731-0.159-1.12-0.159-2.209 0-4 1.791-4 4s1.791 4 4 4h2v3h4v-3h3.5c1.381 0 2.5-1.119 2.5-2.5 0-1.23-0.888-2.253-2.058-2.461zM9 10v3h-2v-3h-2.5l3.5-3.5 3.5 3.5h-2.5z”;
data[“touchPointId”] = group.data.touchPointId;
data[“connectionId”] = group.data.connectionId;
data[“group”]= group.data.key;
data[“type”] = 6;

model.addNodeData(data);

What I have to do?

First, why are you creating a new Array? Create a new Object instead. And better yet, just use JavaScript syntax to build the data object.

  var data = { key: generateUUID(), text: "Post", stroke: "#888", ... };
  model.addNodeData(data);

Second, are you executing this code in an “ExternalObjectsDropped” DiagramEvent listener? Or in a “SelectionMoved” listener?

Third, does your Group have a Group.layout? If not, you might want to locate your extra node appropriately near the originally dropped node, which is selected.

Hi Walter,

Now I have written the code like this in my diagram update event and now two nodes are coming but that extra node is not coming inside the group it is dropping outside the group .I think I have to use the AddMembers function of the group for the new node. But how can I use AddMemebers function and pass the new node there as an argument.Please help me with this code . My code is like this.

var data = { key: generateUUID(), text: “Resume”, stroke: “#888”, color: “#364150”, category: “Default”, geometryString: “M13.942 6.039c0.038-0.174 0.058-0.354 0.058-0.539 0-1.381-1.119-2.5-2.5-2.5-0.222 0-0.438 0.029-0.643 0.084-0.387-1.209-1.52-2.084-2.857-2.084-1.365 0-2.516 0.911-2.88 2.159-0.355-0.103-0.731-0.159-1.12-0.159-2.209 0-4 1.791-4 4s1.791 4 4 4h2v3h4v-3h3.5c1.381 0 2.5-1.119 2.5-2.5 0-1.23-0.888-2.253-2.058-2.461zM9 10v3h-2v-3h-2.5l3.5-3.5 3.5 3.5h-2.5z”, type: 9 };
model.addData(data);

currentGroup.addMembers(currentGroup.diagram.selection, true);

here instead of currentGroup.diagram.selection I have to pass that newly created nodedata but I am confused regarding this.

Why did you not include your original code that assigned the new node data to be a member of the group?
..., group: group.data.key, ...
And also your other properties, touchPointId and connectionId.

Regarding the location of the extra node, I asked whether the Group.layout should be responsible for positioning it, or whether your code should do so explicitly.

I have tried with group:group.data.key but it is not working. And also , touchPointId and connectionId. Buit it is not working.No Group.layout is not responsible for it.

Once you have called Model.addNodeData, you can call Diagram.findNodeForData to get the corresponding Node, whose location or position you can then assign, and you can set its Part.containingGroup to add it to a Group. However, setting the “group” key in the data is exactly what setting Part.containingGroup accomplishes, so I cannot explain that behavior without more details about what you are doing.

Now I have done the code like this

var data = { key: generateUUID(), text: “Resume”, stroke: “#888”, color: “#364150”, category: “Default”, geometryString: “M13.942 6.039c0.038-0.174 0.058-0.354 0.058-0.539 0-1.381-1.119-2.5-2.5-2.5-0.222 0-0.438 0.029-0.643 0.084-0.387-1.209-1.52-2.084-2.857-2.084-1.365 0-2.516 0.911-2.88 2.159-0.355-0.103-0.731-0.159-1.12-0.159-2.209 0-4 1.791-4 4s1.791 4 4 4h2v3h4v-3h3.5c1.381 0 2.5-1.119 2.5-2.5 0-1.23-0.888-2.253-2.058-2.461zM9 10v3h-2v-3h-2.5l3.5-3.5 3.5 3.5h-2.5z”, type: 9, group: currentGroup.data.key };
myGraph.addData(data);

                var addednode = currentGroup.diagram.findNodeForKey(data.key);
                addednode.containingGroup = currentGroup;

but still the new code is not assigning in the group.

Well, you are trying to make the new node be a member of the group twice, in two different ways. So if it is not working either way, you must be using the wrong Group. Check that currentGroup really is the Group that you think it is.