I'm trying to create a breadcrumb without using the url (the route provider) and without using jQuery. I have a tree like that
Humans
Trees
Animals
Cats
Lions
Dogs
Terrier
Bulldog
Cocker
Cars
and I'd like when I click on Cocker to display
Animals / Dogs / Cocker
So, I created a recursive function in order to find parent/parents for each element that I click on but it doesn't work correctly. It finds that an element has a parent, it also finds the first parent of an element but it doesn't show the second parent. For instance instead of
Animals / Dogs / Cocker
it shows
Dogs / Cocker
That's my function
var count = 0;
function iterate(obj) {
for(var key in obj) {
var elem = obj[key];
if(key === "children") {
count++;
}
if(typeof elem === "object") {
if(elem.children === undefined){
elem.children = 1;
}
if(elem.children.length !==1){
iterate(elem);
$scope.showTrail = elem.children;
$scope.elem = elem;
}
}
}
if($scope.elem === undefined){
$scope.elem = {};
$scope.elem.children = {};
$scope.elem.roleName = {};
}
for (var i = 0; i<$scope.elem.children.length; i++) {
if($scope.elem.children[i].roleName === selNode.roleName) {
console.log($scope.elem.roleName + " is a parent of " + selNode.roleName);
}
}
}
iterate($scope.treeData);
and that's the JSON
[
{ "roleName" : "Humans", "roleId" : "role2", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]},
{ "roleName" : "Trees", "roleId" : "role2", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]},
{ "roleName" : "Animals", "roleId" : "role2", "children" : [
{ "roleName" : "Cats", "roleId" : "role11", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]},
{ "roleName" : "Lions", "roleId" : "role11", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]},
{ "roleName" : "Dogs", "roleId" : "role11", "children" : [
{ "roleName" : "Terrier", "roleId" : "role11", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]},
{ "roleName" : "Bulldog", "roleId" : "role11", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]},
{ "roleName" : "Cocker", "roleId" : "role11", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]},
]}
]},
{ "roleName" : "Cars", "roleId" : "role2", "children" : [
{ "roleName" : "", "roleId" : "role11", "children" : [] }
]}
]
Any help please, any idea. Thank you very much.