Subtree collapse results in orphaned nodes without link to its parent

Thanks walter , really appreciate your time and effort . I noticed that in your example , all nodes are expanded initially . But my requirement is not to expand nodes initially because of 200+ nodes . I just added { isTreeExpanded: false } condition in nodeTemplate to make all nodes collapse initially and then none of the nodes are getting expanded on click of button!!!. I debugged expandForm() function , isCollapsed and visible is getting changed but nodes are not getting expanded. Do we need to call cmd.expandTree() function ? Please help

function init() {
if (window.goSamples) goSamples(); // init for these samples – you don’t need to call this
var $ = go.GraphObject.make;

  myDiagram =
    $(go.Diagram, "myDiagramDiv",
      {
        padding: 10,
        layout: $(go.LayeredDigraphLayout,
        { direction: 270, layeringOption: go.LayeredDigraphLayout.LayerLongestPathSource }),
		   //layout: $(go.TreeLayout,
             //   { angle: 270, arrangement: go.TreeLayout.ArrangementFixedRoots }),
        "undoManager.isEnabled": true
      });

  myDiagram.nodeTemplate =
    $(go.Node, go.Panel.Vertical,
	          { portId: "", fromLinkable: true, toLinkable: true },

      new go.Binding("visible"),
            { isTreeExpanded: false },

      $(go.Panel, go.Panel.Auto,
        $(go.Shape,
          { fill: "white", minSize: new go.Size(30, 30), strokeWidth: 0 },
          { cursor: "pointer" },  // indicate that linking may start here
          new go.Binding("fill", "color")),
        $(go.TextBlock,
          { margin: 2 },
          { fromLinkable: false, toLinkable: false },  // don't start drawing a link from the text
          new go.Binding("text", "key"))),

      $("Button",  // a replacement for "TreeExpanderButton" that works for non-tree-structured graphs
        // assume initially not visible because there are no links coming out
        { visible: false },
        // bind the button visibility to whether it's not a leaf node
        new go.Binding("visible", "isTreeLeaf",
          function(leaf) { return !leaf; })
          .ofObject(),
        $(go.Shape,
          {
            name: "ButtonIcon",
            figure: "MinusLine",
            desiredSize: new go.Size(6, 6)
          },
          new go.Binding("figure", "isCollapsed",  // data.isCollapsed remembers "collapsed" or "expanded"
            function(collapsed) { return collapsed ? "PlusLine" : "MinusLine"; })),
        {
          click: function(e, obj) {
            e.diagram.startTransaction();
            var node = obj.part;
            if (node.data.isCollapsed) {
              expandFrom(node, node);
            } else {
              collapseFrom(node, node);
            }
            e.diagram.commitTransaction("toggled visibility of dependencies");
          }
        })
    );

  function collapseFrom(node, start) {
    if (node.data.isCollapsed) return;
    node.diagram.model.setDataProperty(node.data, "isCollapsed", true);
    if (node !== start) node.diagram.model.setDataProperty(node.data, "visible", false);
    node.findNodesOutOf().each(collapseFrom);
  }

  function expandFrom(node, start) {
  console.log(node);
  console.log(start);
    if (!node.data.isCollapsed){
	return;
	}
    node.diagram.model.setDataProperty(node.data, "isCollapsed", false);
    if (node !== start){
	node.diagram.model.setDataProperty(node.data, "visible", true);
	}
	//debugger;
    node.findNodesOutOf().each(expandFrom);
  }

  myDiagram.linkTemplate =
    $(go.Link,
	 { routing: go.Link.AvoidsNodes },
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" }));

  myDiagram.model = new go.GraphLinksModel([
    { key: "B", color: "gray", },
    { key: "C", color: "orange", },
    { key: "D0", color: "gray",  },
    { key: "D1", color: "orange",  },
    { key: "D2", color: "orange",  },
    { key: "D3", color: "gray",  },
  ], [
      { from: "B", to: "D0" },
      { from: "B", to: "D1" },
      { from: "D0", to: "C" },
      { from: "D1", to: "D2" },
      { from: "D2", to: "C" },
	  { from: "C", to: "D2" },
	  { from: "D0", to: "D3" }
    ]);
}

As I said before, if you want to implement your own criteria for expanding and collapsing nodes and associated links and nodes, you must not use Node.isTreeExpanded or any of the Node methods or other properties that involve identifying/navigating/collapsing/expanding trees. You have to implement the state on the node data (rather than use the built-in properties) and you have to implement the collapse and expand functionality to do exactly what you want. Otherwise the standard policy and your policy will get confused.

That has been done in that Custom Collapse Expand sample. It still isn’t clear to me that its policies aren’t what you want.