如何在 GOJS 的模板中获取节点数据?

How to get the node data, inside the template in GOJS?

我只想在数据中的文本等于特定字符串时创建几个文本块。 如果不是 - 我只想创建一个文本块。

var template = GO(go.Node, "Auto",{desiredSize: new go.Size(width, height) },     
                  GO(go.Shape, shapeMap.getValue(shape), new go.Binding("fill", "fill")),  
                  ( ???? .includes("[UMS]")) ?
                        GO(go.Panel, "Vertical",
                            GO(go.TextBlock,{font: "7pt serif"}, new go.Binding("stroke", "color"), new go.Binding("text", "txtPart1")),
                            GO(go.TextBlock,{text: "[UMS]", font: "7pt serif", click: function(e, obj) {window.open("https://" + obj.part.data.key + ":8090")}}, new go.Binding("stroke", "color")),
                            GO(go.TextBlock,{font: "7pt serif"}, new go.Binding("stroke", "color"), new go.Binding("text", "txtPart2")))
                        :
                           GO(go.TextBlock,{font: "7pt serif"}, new go.Binding("stroke", "color"), new go.Binding("text", "txtPart1"))

                       );

如何测试 data.text 是否包含?

(我知道如何把它放到里面 function:click: function(e, obj) {return obj.part.data.key }

或如何声明它 - 使用绑定 - 因此数据将是每个节点而不是每个模板。 但在模板内的代码中??)

您可以添加数据绑定,将 TextBlockPanel 的可见性绑定到 data.text。这是一个这样的例子:

      new go.Binding("visible", "text", function(textvalue) {
        return (textvalue.indexOf("[UMS]") >= 0);
      })

在那个例子中,如果文本中不包含"[UMS]",则具有此绑定的GraphObject将不可见(不会显示,也不会占用space) .

这是一个完整的例子:http://codepen.io/simonsarris/pen/jPzyoa?editors=001

后代的整个模板:

myDiagram.nodeTemplate =
  $(go.Node, "Vertical",
    new go.Binding('background', 'color'),
    $(go.TextBlock,
      { margin: 3 },
      new go.Binding("text", "key")),
    // This textblock will be hidden if the data.text does not contain "three"
    $(go.TextBlock,
      { margin: 3 },
      new go.Binding("text", "text"),
      new go.Binding("visible", "text", function(textvalue) {
        return (textvalue.indexOf('three') >= 0);
      })
     )
  );

您可以在此处阅读有关数据绑定和转换函数的更多信息:http://gojs.net/latest/intro/dataBinding.html