dolor3sh4ze
dolor3sh4ze

Reputation: 1155

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

Upvotes: 0

Views: 392

Answers (1)

Simon Sarris
Simon Sarris

Reputation: 63812

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.

Upvotes: 1

Related Questions