Unable to search next nodes by text

I have added Search Button with search textbox to end of Go JJS Diagram. I am able to Search Nodes first time. But unable to Search next matching node by again clicking on Search button multiple times with same search text.

Code :


        function searchDiagram() {  // called by button
            var input = document.getElementById("mySearch");
            if (!input) return;
            myDiagram.focus();

            myDiagram.startTransaction("highlight search");

            if (input.value) {
                // search four different data properties for the string, any of which may match for success
                // create a case insensitive RegExp from what the user typed
                var safe = input.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
                var regex = new RegExp(safe, "i");
                var results = myDiagram.findNodesByExample({ text: regex },
                    { nation: regex },
                    { title: regex },
                    { headOf: regex });
                myDiagram.highlightCollection(results);
                // try to center the diagram at the first node that was found
                if (results.count > 0) myDiagram.centerRect(results.first().actualBounds);
            } else {  // empty string only clears highlighteds collection
                myDiagram.clearHighlighteds();
            }

            myDiagram.commitTransaction("highlight search");
        }
 

If your HTML button is calling your searchDiagram function every time, then every time it will do the search and highlight the (probably) same resulting collection of nodes.

I think you are asking about how to sometimes get the behavior of the spacebar key. That keyboard event calls CommandHandler.scrollToPart with no argument. CommandHandler | GoJS API

So you will need to change your HTML button’s click function to call myDiagram.commandHandler.scrollToPart() when you want that behavior.

1 Like