【问题标题】:Get location after moving node移动节点后获取位置
【发布时间】:2019-08-12 09:19:30
【问题描述】:

移动节点后如何获取当前位置以保存到数据库新位置。

myDiagram.addDigramListener("SelectionMoved",
  function(e) {
    let part = e.subject.part;
    console.log(part)
  }
)

但是该部分总是为空,为什么?

【问题讨论】:

  • 您能否创建工作 sn-p(编辑器或 codepen、jsfiddle 等中的[<>] 图标)来证明您的问题?似乎e.subject 与您的预期不同

标签: javascript gojs


【解决方案1】:

我能够使用以下代码获取新的位置和密钥


myDiagram.addDiagramListener("SelectionMoved", function(event) {

  // https://gojs.net/latest/api/symbols/Part.html#location
  // * PART
  var selectedNode = event.diagram.selection.first();

  console.log("selectedNode",selectedNode);
  console.log("selectedNodeKey",selectedNode.key);
  console.log("selectedNode", selectedNode.location.toString());
  console.log("selectedNode", selectedNode.location.x);
  console.log("selectedNode", selectedNode.location.y);
  console.log("locationObject", selectedNode.locationObject);

  //Save new location
  // key: selectedNode.key
  // location-x: selectedNode.location.x
  // location-y: selectedNode.location.y

});

【讨论】:

    【解决方案2】:

    e.subject 是一个集合,是所有移动的零件的选择。移动的部分可能不止一个。

    如果你有理由确定只有一个部分被移动,你可以这样写:

    myDiagram.addDigramListener("SelectionMoved",
      function(e) {
        let part = e.subject.first();
        console.log(part.toString())
      }
    )
    

    但是,如果您只想将位置保存到数据库中,为什么不在位置上进行双向数据绑定呢? flowchart example 证明了这一点:

    // The Node.location comes from the "loc" property of the node data,
    // converted by the Point.parse static method.
    // If the Node.location is changed, it updates the "loc" property of the node data,
    // converting back using the Point.stringify static method.
    new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2018-02-07
      • 2011-10-28
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多