Janae Fairbourn
Janae Fairbourn

Reputation: 1

GoJs close all roots in one click

I'm new to GoJs. I've built an Org chart that has search functionality. When the user searches, all branches that contain a match are expanded to the match. When they hit the "clear" button, all branches should collapse. It does collapse down to the third level like I want it to, however the internal roots are still expanded. So if I open a branch, all of the internal branches that were opened on the previous search are still expanded.

Here's the code right now:

function collapseAllNodes() {
var input = document.getElementById('mySearch');
if (input) input.value = ''; // clear anything out of the mySearch div
myDiagram.startTransaction('collapse all'); // Collaps roots
myDiagram.findTreeRoots().each(function (root) {
root.collapseTree(3); // Collapse until the third level
});
myDiagram.commitTransaction('collapse all');
}

Is there a way to iterate through and make sure every branch is closed completely? Please let me know if I need to clarify better. Thank you

Upvotes: 0

Views: 32

Answers (1)

Janae Fairbourn
Janae Fairbourn

Reputation: 1

I've fixed it by adding a few lines of code, I'll share them below in case someone else wonders in the future.

function collapseAllNodes() {
        var input = document.getElementById('mySearch');
        if (input) input.value = ''; // clear anything out of the mySearch 
        myDiagram.startTransaction('collapse all'); // Collaps roots
        myDiagram.findTreeRoots().each(function (root) {
          root.collapseTree(3); // Collapse until the third level
          myDiagram.nodes.each(function(root) {
            root.wasTreeExpanded = false;
          });
          myDiagram.clearHighlighteds();
        });
        myDiagram.commitTransaction('collapse all');
      }

Upvotes: 0

Related Questions