2015-06-17 26 views
1

我有一个名为域的父节点,我们可以在它下面添加多个子节点,并且在每个子节点下我们可以添加多个子子节点。我想创建一个结构类似这样的有没有什么方法创建一个哈希映射像结构

enter image description here

我想下选择业务域添加subchild。它工作正常进行的第一要素,但是当我在任何其他业务领域增加subchild选择它增加了这样创建的所有subchild:

enter image description here

这是我服务方法我在哪里存储subchild列表:

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'; 
       } 
    }, 
} 

我得到这种类型的结构为childSubDomainName

enter image description here

任何人都可以请建议我如何做到这一点。我尝试了很多东西,但没有奏效。

+0

什么是你'for'循环点维持? – Phil

+0

我添加了for循环来检查正在添加的名称是否是唯一的 –

+0

但除了继续重新分配'index'外,没有其他任何操作,因此导致'index'只与最后一个'childSubDomainName'有关 – Phil

回答

1

您用于将对象插入到层次结构中的循环构造应涉及检查子项需要插入的位置。这将涉及两个条件检查: 1.对于父名称 2.对于在该父对象下存在的子项。 3.如果上述两个条件都匹配,则子小孩应该插入相应的父小孩级别。

的层次结构可以在follwing结构

[ 
    { 
     "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" 
        } 
       ] 
      } 
     ] 
    } 
] 
相关问题