【问题标题】:How to get the node data, inside the template in GOJS?如何在 GOJS 的模板中获取节点数据?
【发布时间】:2015-07-06 11:42:09
【问题描述】:

只有当数据中的文本等于特定字符串时,我才想创建一些 textBlock。 如果没有 - 我只想创建一个 textBlock。

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 是否包含?

(我知道如何在函数中获取它:点击:函数(e,obj){return obj.part.data.key}

或者如何声明它 - 使用绑定 - 所以数据将是每个节点而不是每个模板。 但是在模板里面的代码里??)

【问题讨论】:

    标签: javascript gojs


    【解决方案1】:

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

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

    在该示例中,如果文本不包含"[UMS]",则具有此绑定的 GraphObject 将不可见(不会显示,也不会占用空间)。

    这是一个完整的例子: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

    【讨论】:

    • 我尝试在绑定中使用该函数,但我没有在可见属性中使用它的想法。很酷&很棒。 tnx.
    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多