0

这是网球抽签,我需要能够再见,所以大多数父母都有两个孩子,即。每场比赛的获胜者都会通过,但在某些情况下会轮空,因此只有一个孩子。请参阅此处作为示例,其中一些父匹配项没有子项,而有些匹配项有一个: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;
    }
}
4

1 回答 1

0

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

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++;
    }
  }
}

你可以在这里玩。这个想法是检查每个孩子是否是再见。如果是,我们将其从列表中删除,如果不是,我们递归并删除所有后代子再见。

于 2013-02-14T20:17:58.567 回答