2016-08-24 7 views
1

jsonデータを追加してツリービュー構造を作成したいとします。ここでツリービュー構造に子レベルと大子レベルのjsonデータを追加する方法

var json = { 
 
    "category": [{ 
 
     "title": "Customer Satisfaction", 
 
     "id": "nnanet:category/certified-pre-owned", 
 
     "items": [{ 
 
      "title": "Bulletins", 
 
      "id": "nnanet:category/customer-satisfaction/bulletins", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }, { 
 
      "title": "Consumer Affairs", 
 
      "id": "nnanet:category/customer-satisfaction/consumer-affairs" 
 
     }, { 
 
      "title": "Loyalty", 
 
      "id": "nnanet:category/customer-satisfaction/loyalty", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }] 
 
    }, { 
 
     "title": "Retailer Digital Marketing", 
 
     "id": "nnanet:category/retailer-digital-marketing", 
 
     "items": [{ 
 
      "title": "TOI", 
 
      "id": "nnanet:category/retailer-digital-marketing/toi", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }, { 
 
      "title": "Basics", 
 
      "id": "nnanet:category/retailer-digital-marketing/reference-guide/basics" 
 
     }, { 
 
      "title": "International", 
 
      "id": "nnanet:category/retailer-digital-marketing/international" 
 
     }] 
 
    }, { 
 
     "title": "Finance Today", 
 
     "id": "nnanet:category/customer-satisfaction/bulletins/finance-today", 
 
     "items": [{ 
 
      "title": "TOI", 
 
      "id": "nnanet:category/retailer-digital-marketing/toi", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }, { 
 
      "title": "Basics", 
 
      "id": "nnanet:category/retailer-digital-marketing/reference-guide/basics" 
 
     }, { 
 
      "title": "International", 
 
      "id": "nnanet:category/retailer-digital-marketing/international" 
 
     }] 
 
    }, { 
 
     "title": "Annual", 
 
     "id": "nnanet:category/customer-satisfaction/bulletins/finance-today/revenue/annual", 
 
     "items": [{ 
 
      "title": "TOI", 
 
      "id": "nnanet:category/retailer-digital-marketing/toi" 
 
     }, { 
 
      "title": "Basics", 
 
      "id": "nnanet:category/retailer-digital-marketing/reference-guide/basics" 
 
     }, { 
 
      "title": "International", 
 
      "id": "nnanet:category/retailer-digital-marketing/international" 
 
     }] 
 
    }] 
 
}; 
 
    function expander(){ 
 
     var tree = document.querySelectorAll('ul.tree a:not(:last-child)'); 
 
    for(var i = 0; i < tree.length; i++){ 
 
     tree[i].addEventListener('click', function(e) { 
 
      var element = e.target.parentElement; //actually this is just the elem itself 
 
      var parent = element.parentElement 
 

 
      var opensubs = parent.querySelectorAll(':scope .open'); 
 
      console.log(opensubs); 
 
      var classList = element.classList; 
 
      if(opensubs.length !=0) { 
 
        for(var i = 0; i < opensubs.length; i++){ 
 
        opensubs[i].classList.remove('open'); 
 
       } 
 
      } 
 
       classList.add('open'); 
 

 
     }); 
 
    } 
 
    } 
 
$(function(){ 
 
\t \t var tree = $("ul.tree"); 
 
    $.each(json.category,function(category){ 
 
    \t var categoryValue = json.category[category]; 
 
    \t tree.append('<li><a href="#">'+categoryValue.title+'</a><ul></ul></li>'); 
 
     var el = tree.children("li").children("ul"); 
 
     $.each(categoryValue.items,function(itemId){ 
 
     \t var item = categoryValue.items[itemId]; 
 
     $(el[category]).append('<li><a href="#">'+item.title+'</a></li>'); 
 
     if(item.thirditems){ 
 
     \t $(el[category]).children("li").append('<ul></ul>'); 
 
      var el1 = el.children("li").children("ul"); 
 
      $.each(item.thirditems,function(thirdItemId){ 
 
      var thirdItem = item.thirditems[thirdItemId]; 
 
      console.log(el1[itemId]); 
 
      $(el1[itemId]).append('<li><a href="#">'+thirdItem.title+'</a><ul></ul></li>'); 
 
      }); 
 
     } 
 
     
 
     }); 
 
    }); 
 
    expander(); 
 
});
body { 
 
    font-family: Arial; 
 
} 
 

 
ul.tree li { 
 
    list-style-type: none; 
 
    position: relative; 
 
} 
 

 
ul.tree li ul { 
 
    display: none; 
 
} 
 

 
ul.tree li.open > ul { 
 
    display: block; 
 
} 
 

 
ul.tree li a { 
 
    color: black; 
 
    text-decoration: none; 
 
} 
 

 
ul.tree li a:before { 
 
    height: 1em; 
 
    padding:0 .1em; 
 
    font-size: .8em; 
 
    display: block; 
 
    position: absolute; 
 
    left: -1.3em; 
 
    top: .2em; 
 
} 
 

 
ul.tree li > a:not(:last-child):before { 
 
    content: '+'; 
 
} 
 

 
ul.tree li.open > a:not(:last-child):before { 
 
    content: '-'; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<ul class="tree"></ul>

私は3つのレベルJSONデータを追加するための課題を持っているし、2つのレベルのJSON件まで適切に追加:当初、私は、このJSONツリービューで自分のフィドルコードは、静的なツリービューを作成していました。 thirditemの状態を直面して使用した後。 私のjsonコードにはthirditemsという名前の配列があります。このthirditems配列を対応する親配列に追加する方法は?

正確に小売店内digital marketing > sub-ToI > semi-sub-TOI > super-sub-TOIすべてthirditems json配列が最初の値にのみ追加されています。 2レベルのjsonデータが正しく追加されていますが、3レベルのjsonデータを追加する方法はありますか?詳細情報については

このフィドルをチェック:https://jsfiddle.net/ak3zLzgd/6/

答えて

0

私はちょうど

var el1 = el.children("li").children("ul"); 
LINE-以下を変更したチェックthis-

var json = { 
 
    "category": [{ 
 
     "title": "Customer Satisfaction", 
 
     "id": "nnanet:category/certified-pre-owned", 
 
     "items": [{ 
 
      "title": "Bulletins", 
 
      "id": "nnanet:category/customer-satisfaction/bulletins", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }, { 
 
      "title": "Consumer Affairs", 
 
      "id": "nnanet:category/customer-satisfaction/consumer-affairs" 
 
     }, { 
 
      "title": "Loyalty", 
 
      "id": "nnanet:category/customer-satisfaction/loyalty", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }] 
 
    }, { 
 
     "title": "Retailer Digital Marketing", 
 
     "id": "nnanet:category/retailer-digital-marketing", 
 
     "items": [{ 
 
      "title": "TOI", 
 
      "id": "nnanet:category/retailer-digital-marketing/toi", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }, { 
 
      "title": "Basics", 
 
      "id": "nnanet:category/retailer-digital-marketing/reference-guide/basics" 
 
     }, { 
 
      "title": "International", 
 
      "id": "nnanet:category/retailer-digital-marketing/international" 
 
     }] 
 
    }, { 
 
     "title": "Finance Today", 
 
     "id": "nnanet:category/customer-satisfaction/bulletins/finance-today", 
 
     "items": [{ 
 
      "title": "TOI", 
 
      "id": "nnanet:category/retailer-digital-marketing/toi", 
 
      "thirditems": [{ 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }, { 
 
       "title": "TOI", 
 
       "id": "nnanet:category/retailer-digital-marketing/toi" 
 
      }] 
 
     }, { 
 
      "title": "Basics", 
 
      "id": "nnanet:category/retailer-digital-marketing/reference-guide/basics" 
 
     }, { 
 
      "title": "International", 
 
      "id": "nnanet:category/retailer-digital-marketing/international" 
 
     }] 
 
    }, { 
 
     "title": "Annual", 
 
     "id": "nnanet:category/customer-satisfaction/bulletins/finance-today/revenue/annual", 
 
     "items": [{ 
 
      "title": "TOI", 
 
      "id": "nnanet:category/retailer-digital-marketing/toi" 
 
     }, { 
 
      "title": "Basics", 
 
      "id": "nnanet:category/retailer-digital-marketing/reference-guide/basics" 
 
     }, { 
 
      "title": "International", 
 
      "id": "nnanet:category/retailer-digital-marketing/international" 
 
     }] 
 
    }] 
 
}; 
 
    function expander(){ 
 
     var tree = document.querySelectorAll('ul.tree a:not(:last-child)'); 
 
    for(var i = 0; i < tree.length; i++){ 
 
     tree[i].addEventListener('click', function(e) { 
 
      var element = e.target.parentElement; //actually this is just the elem itself 
 
      var parent = element.parentElement 
 

 
      var opensubs = parent.querySelectorAll(':scope .open'); 
 
      console.log(opensubs); 
 
      var classList = element.classList; 
 
      if(opensubs.length !=0) { 
 
        for(var i = 0; i < opensubs.length; i++){ 
 
        opensubs[i].classList.remove('open'); 
 
       } 
 
      } 
 
       classList.add('open'); 
 

 
     }); 
 
    } 
 
    } 
 
$(function(){ 
 
\t \t var tree = $("ul.tree"); 
 
    $.each(json.category,function(category){ 
 
    \t var categoryValue = json.category[category]; 
 
    \t tree.append('<li><a href="#">'+categoryValue.title+'</a><ul></ul></li>'); 
 
     var el = tree.children("li").children("ul"); 
 
     $.each(categoryValue.items,function(itemId){ 
 
     \t var item = categoryValue.items[itemId]; 
 
     $(el[category]).append('<li><a href="#">'+item.title+'</a></li>'); 
 
     if(item.thirditems){ 
 
     \t $(el[category]).children("li").append('<ul></ul>'); 
 
      var el1 = $(el[category]).children("li").children("ul"); 
 
      $.each(item.thirditems,function(thirdItemId){ 
 
      var thirdItem = item.thirditems[thirdItemId]; 
 
      
 
      $(el1[itemId]).append('<li><a href="#">'+thirdItem.title+'</a><ul></ul></li>'); 
 
      }); 
 
     } 
 
     
 
     }); 
 
    }); 
 
    expander(); 
 
});
body { 
 
    font-family: Arial; 
 
} 
 

 
ul.tree li { 
 
    list-style-type: none; 
 
    position: relative; 
 
} 
 

 
ul.tree li ul { 
 
    display: none; 
 
} 
 

 
ul.tree li.open > ul { 
 
    display: block; 
 
} 
 

 
ul.tree li a { 
 
    color: black; 
 
    text-decoration: none; 
 
} 
 

 
ul.tree li a:before { 
 
    height: 1em; 
 
    padding:0 .1em; 
 
    font-size: .8em; 
 
    display: block; 
 
    position: absolute; 
 
    left: -1.3em; 
 
    top: .2em; 
 
} 
 

 
ul.tree li > a:not(:last-child):before { 
 
    content: '+'; 
 
} 
 

 
ul.tree li.open > a:not(:last-child):before { 
 
    content: '-'; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<ul class="tree"></ul>

それを考え出しました

Into-

var el1 = $(el[category]).children("li").children("ul"); 

はここfiddle更新されます。

+0

ありがとうございました.......すばらしい仕事!!!!!!!!!!!!!!!! 11 –

+0

まだそれを取っていない –

+0

これをチェック:https://jsfiddle.net/ak3zLzgd/14/ –

関連する問題