[英]findNodesByExample() with Array Go.JS
I search for a solution since almost 3 hours now without find a valid solution.我从近 3 个小时以来一直在寻找解决方案,但没有找到有效的解决方案。
I would like to findNodesByExample a name in my object array who match with a regex.我想在我的 object 数组中找到与正则表达式匹配的名称。 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.当我搜索一个键时, findNodesByExample() 会返回所有与正则表达式匹配的对象。 But for the fields, it return an empty array... For create this function, I followed the Go.JS API doc with the age example但是对于字段,它返回一个空数组...为了创建这个 function,我遵循了Go.JS API 文档和年龄示例
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.这会根据与正则表达式匹配的 node.data.fields.name 找到一个节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.