nickperkinslondon / angular-bootstrap-nav-tree

An AngularJS directive that creates a Tree based on a Bootstrap "nav" list.
MIT License
681 stars 301 forks source link

How to delete a branch #106

Closed hegoku closed 5 years ago

hegoku commented 8 years ago

I can't find a delete function in source code... Is there any solutions? Thanks!

guicoma commented 8 years ago

To have a function in the directive (just like add_branch) would be great.

For the meantime I worked around this by travelling the tree and replacing the data:

function deleteTreeItems(tree, items) {
  for (var i = 0; i < tree.length; i++) {
    for (var j = 0; j < items.length; j++) {
      if (tree[i].id == items[j].id) {
        tree.splice(i,1);
        i = 0;
      } else {
        if (tree[i].children.length > 0) {
          tree[i].children = deleteTreeItems(tree[i].children, items);
        }
      }
    }
  }
  return tree;
}
hegoku commented 8 years ago

Yep, Great!

On 7 September 2016 at 18:11, guicoma notifications@github.com wrote:

To have a function in the directive (just like add_branch) would be great.

For the meantime I worked around this by travelling the tree and replacing the data:

function deleteTreeItems(tree, items) { for (var i = 0; i < tree.length; i++) { for (var j = 0; j < items.length; j++) { if (tree[i].id == items[j].id) { tree.splice(i,1); i = 0; } else { if (tree[i].children.length > 0) { tree[i].children = deleteTreeItems(tree[i].children, items); } } } } return tree; }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nickperkinslondon/angular-bootstrap-nav-tree/issues/106#issuecomment-245236262, or mute the thread https://github.com/notifications/unsubscribe-auth/AErkbyLzXSgfwzdfkFY2lh3gXyiBqKNCks5qno3qgaJpZM4JdcqJ .

rbw commented 7 years ago

Hello,

There's already a PR for this, so I'm not gonna create another. Anyway, I did it like this.

tree.remove_branch = function(branch) {
  if (branch != null) {
    var parent = get_parent(branch);
    // Make sure we have a parent
    if(parent != null) {
      parent.children.splice(parent.children.indexOf(branch), 1);
    }
  }
};
daerogami commented 5 years ago

Duplicate of #36