I have a parent named domain and we can add multiple child beneath it and under every child we can add multiple sub child. I want to create a structure like this
I want to add a subchild under The business domain selected. It is working fine for the first element but when I add a subchild under any other Business domain selected It adds all the subchild created like this:
This is my service method where I am storing the subchild list:
app.factory('DomainNameService',['$q', function($q) {
var childSubDomainName=[];
setBusSubDomain:function(val,busDomain){//In val I am getting the business
//domain selected and in busDomain I am getting the name entered in name field
if(childSubDomainName.length<1){
childSubDomainName.push(busDomain);
}
else {
for(var i=0;i<childSubDomainName.length;i++){
var index = childSubDomainName[i].name.indexOf(busDomain.name);//Added this for unique name check
}
if(index==-1){//If the name is unique then i allow it to add to childSubDomainName
//Here I want to implement a logic that busDomain gets add only to the parent selected but I am not getting how to do this
childSubDomainName.push(busDomain);
}
else
this.error = 'Name already in use';
}
},
}
I am getting this type of structure for childSubDomainName
Can any one please suggest me how to do this. I have tried many things but it didn't worked.
The looping construct you are using for inserting the objects in the heirarchy should involve the checking as to where exactly the child needs to inserted. This will involve two condition checks: 1. for the parent name 2. for the child existing under that parent object. 3. if both the above conditions matches only then the sub child should be inserted in the respective parent child heirarchy.
The heirarchy can be maintained in the follwing structure
[
{
"parentid": "id",
"parentName": "name1",
"children": [
{
"childid": "c_id1",
"childname": "cn1",
"subchild": [
{
"sbid": "id",
"sbname": "name"
},
{
"sbid": "id",
"sbname": "name"
}
]
},
{
"childid": "c_id2",
"childname": "cn2",
"subchild": [
{
"sbid": "id",
"sbname": "name"
},
{
"sbid": "id",
"sbname": "name"
}
]
}
]
},
{
"parentid": "id",
"parentName": "name1",
"children": [
{
"childid": "c_id1",
"childname": "cn1",
"subchild": [
{
"sbid": "id",
"sbname": "name"
},
{
"sbid": "id",
"sbname": "name"
}
]
},
{
"childid": "c_id2",
"childname": "cn2",
"subchild": [
{
"sbid": "id",
"sbname": "name"
},
{
"sbid": "id",
"sbname": "name"
}
]
}
]
}
]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments