【问题标题】:How to remove a single child node in tree layout如何在树布局中删除单个子节点
【发布时间】:2013-02-15 04:17:58
【问题描述】:

这是网球抽签的,我需要能够再见,所以大多数父母都有两个孩子,即。每场比赛的获胜者都会通过,但在某些情况下会轮空,因此只有一个孩子。请参阅此处作为示例,其中一些父匹配项没有子项,而有些匹配项有一个:http://www.irtpa.com/index.php/realtennis/ladder/1246

我认为这个答案没有帮助:How to remove node on tree layout D3.js?

假设节点的所有子节点都被隐藏/删除。

根据上面的 stackoverflow 答案,我已经走了这么远,但我的大脑看不到删除/隐藏孩子的解决方案:

function check_children(data, parent) {
// recurse through array and toggle/hide any Match Byes

  if (typeof data.data != "undefined") {
    if (data.data.bye == "byeM") {
        toggle(parent);
    }
  }

  if (data.children) {
    check_children(data.children[0], data);
    check_children(data.children[1], data);
  }

}

function toggle(d) {
    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    }
}

【问题讨论】:

  • 我不确定我是否理解您想要做什么?你想提拔孩子代替父母吗?摆脱使用 _children 的切换行应该从树中永久删除这些孩子。也许你可以为你正在寻找的东西发布一个 jsFiddle 和/或一些前后 json 对象?
  • 我只想删除其中一个孩子,而不是像大多数示例所示的那样同时删除。所以 _children 技巧不起作用。这是一段相当重要的代码,所以希望有人会说 - 这很容易,只是......!解决这个问题后,我可能会尝试另一种方法,看看是否可以使用布局中的分隔将一个隐藏在另一个之下。
  • 好的 - 所以当你调用 toggle(parent) 时,你想删除父级的特定子级?或者从包含它的子列表中删除父?
  • 移除一个特定的孩子。

标签: d3.js


【解决方案1】:

如果你想删除你的“再见”孩子,我可能会在递归之前进行测试。所以像:

function removeByeChildren(parent) {
  if(!parent.children)
    return;

  var i = 0;
  while(i < parent.children.length) {
    var data = parent.children[i].data;
    if (typeof data != "undefined" && data.bye == "byeM") {
      // remove child - could save it in _children if you wanted to ever put it back
      var child = parent.children.splice(i,1);
      // length of child list reduced, i points to the next child
    }
    else {
      // not a bye - recurse
      removeByeChildren(parent.children[i]);
      // all the child's bye's are removed so go to the next child
      i++;
    }
  }
}

您可以使用它here。这个想法是检查每个孩子是否是再见。如果是,我们将其从列表中删除,如果不是,则递归并删除所有后代子节点。

【讨论】:

  • 这看起来很有希望。早上会去。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 2012-01-07
  • 2017-12-07
相关资源
最近更新 更多