4

Fiddle link where we can add new child nodes, and their child node too, can delete the child node form the parent ONLY.

I am trying to delete all the child node and ALSO the current node from the same delete button. (All child node are deleted successfully but i am not able to delete the current node from the list.)

How can i do this via Angular Js.

    angular.module("myApp", []).
controller("TreeController", ['$scope', function($scope) {
    $scope.delete = function(data) {
        data.nodes = [];
    };
    $scope.add = function(data) {
        var post = data.nodes.length + 1;
        var newName = data.name + '-' + post;
        data.nodes.push({name: newName,nodes: []});
    };
    $scope.tree = [{name: "Node", nodes: []}];
}]);

Can anyone please help.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
user875123
  • 137
  • 8
  • With your current function setup, I don't think you can.. I might be wrong. – Billy Apr 30 '15 at 09:29
  • So is there any other way? like can i reassign $scope.tree = $scope.tree by removing particular deleted triggered node – user875123 Apr 30 '15 at 09:32
  • Also see http://stackoverflow.com/a/14460332/123415 and http://jsfiddle.net/sunnycpp/KNM4q/110/ – jcuenod Apr 30 '15 at 09:36
  • possible duplicate of [Recursion in Angular directives](http://stackoverflow.com/questions/14430655/recursion-in-angular-directives) – jcuenod Apr 30 '15 at 09:37

1 Answers1

2

To remove the current node also, you need to remove it from the parent, therefore you will need to store reference to parent node in data

data.nodes.push({name: newName,nodes: [], parent: data});

Then in delete(), you can access parent node and remove itself:

$scope.delete = function(data) {
    var index = data.parent.nodes.indexOf(data);
    if(index > -1) {
        data.parent.nodes.splice(index, 1);
    }
};

Updated jsfiddle

Phuong Nguyen
  • 2,960
  • 1
  • 16
  • 25