如何在不删除子节点的情况下删除父节点

How to delete the parent node without deleting the children nodes

这是我在 reactjs 中使用的代码示例。

const node = graph.$(`#${selectedNode.id()}`);
      graph.remove(node);

selectedNode.id是父节点的id但是它删除了这个父节点里面的所有子节点。 如何只删除父节点而不删除其子节点?

这个问题与这里的问题类似Remove/hide compound node without removing/hiding descendants but I will appreciate it if some code samples are provided because in the doc here http://js.cytoscape.org/#collection/graph-manipulation/eles.move我们有一个边缘的浅代码示例,但我对节点感兴趣。

谢谢

您可以删除一个父节点,方法是先将其子节点移动到父节点的父节点(如果存在,否则您应该分配一个空值),然后删除父节点。 Select 父节点并单击下面示例中的删除按钮。

var cy = window.cy = cytoscape({
  container: document.getElementById('cy'),
  layout: {name: 'grid', rows: 2},
  style: [{
      selector: 'node',
      css: {
        'label': 'data(id)'     
        }
    }
  ],
  elements: {
    nodes: [{
        data: {
          id: 'n0',
          parent: 'n1'
        }
      },
      {
        data: {
          id: 'n1',
          parent: 'n2'
        }
      },
      {
        data: {
          id: 'n2'
        }
      },
      {
        data: {
          id: 'n3'
        }
      }
    ],
    edges: [
      {
        data: {
          id: 'n2n3',        
          source: 'n2',
          target: 'n3',
          weight: 7
        }
      }
    ]
  }
});

document.getElementById("deleteButton").addEventListener("click", function() {
  let selected = cy.nodes(':selected')[0];
  selected.children().move({parent : (selected.parent().id() ? selected.parent().id() : null)});
  selected.remove();
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#button {
  z-index = 1000;
}

#cy {
  height: 95%;
  width: 95%;
  left: 0;
  top: 50;
  z-index = 900;
  position: absolute;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://unpkg.com/cytoscape@3.10.0/dist/cytoscape.min.js">
  </script>
</head>

<body>
  <button id="deleteButton" type="button">Delete selected</button>
  <div id="cy"></div>
</body>

</html>