Search code examples
javascriptgojs

findNodesByExample() with Array Go.JS


I search for a solution since almost 3 hours now without find a valid solution.

I would like to findNodesByExample a name in my object array who match with a regex. This name is in an array, who is in the Node (example below):

[ {
     key: "avidyne_abs_nav",
     fields: [{
                name: "avidyne.abs_nav.A429AVIDYNE"
              }]
} ]

So, to search for names who match with my regex, here is my code:

let safe = input.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let regex = new RegExp(safe, "i");
myDiagram.findNodesByExample({ key: regex },
                             { fields: function(n) { return n.some(a => { regex.test(a.name) }) } });

When I search a key, findNodesByExample() return me all objects who matchs with regex. But for the fields, it return an empty array... For create this function, I followed the Go.JS API doc with the age example


Solution

  • I'm not sure what your input is but it seems to work as I would expect:

      function init() {
        var $ = go.GraphObject.make ;  // for conciseness in defining templates
        myDiagram = new go.Diagram("myDiagramDiv");
    
        myDiagram.model = new go.GraphLinksModel(
          [{
            key: "avidyne_abs_nav",
            fields: [{
              name: "avidyne.abs_nav.A429AVIDYNE"
            }]
          }]);
    
    
      } // end init
    
      window.click = function() {
        let safe = "A429AVIDYNE"; //input.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
        let regex = new RegExp(safe, "i");
        const results = myDiagram.findNodesByExample({ key: regex },
          {
            fields: function (n) {
              return n.some(a => {
                return regex.test(a.name)
              }
              )
            }
          });
        console.log(results.count); // found one node
      }
    

    This finds the one node, based on the node.data.fields.name matching the regex.