2016-05-29 42 views
1

嵌套的樹我有這個JSON,我需要證明的這三個節點樹角JS:data.key & data.parentItem & data.title。顯示JSON與角

這是我的js代碼:

var phonecatApp = angular.module('myApp', []) 
 
phonecatApp.controller('myController', function myController($scope, $http) { 
 
    $http.get('https://api.zotero.org/users/475425/collections/9KH9TNSJ/items?format=json') 
 
    .then(function (response) { 
 
     var data = response.data 
 

 
     data = data.filter(function (obj) { 
 
      return true 
 
     }) 
 
     .map(function (obj) { 
 
      return { 
 
      key: obj.key, 
 
      parentItem: obj.data.parentItem, 
 
      title: obj.data.title 
 
      } 
 
     }) 
 

 
     var log = [] 
 
     var parent = angular.forEach(data, function (value, key) { 
 
     if (value.parentItem === undefined) { 
 
      this.push(value) 
 
     } 
 
     }, log) 
 
     $scope.paR = log 
 

 
     var nlog = [] 
 
     var children = angular.forEach(data, function (value, key) { 
 
     if (value.parentItem !== undefined) { 
 
      this.push(value) 
 
     } 
 
     }, nlog) 
 
     $scope.chilD = nlog 
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html ng-app="myApp"> 
 

 
<head> 
 
    <title>Hello</title> 
 
    <link href=" css/bootstrap.min.css " type="text/css " rel="stylesheet "> 
 
    <link href="themes/default/style.min.css " type="text/css " rel="stylesheet "> 
 
    <link href="css/angular-json-human.min.css" type="text/css" rel="stylesheet"> 
 
</head> 
 

 
<body ng-controller="myController "> 
 
    <div> 
 
    <ul ng-repeat="myJ in paR"> 
 
     <li> 
 
     <h3>{{myJ.key}}</h3></li> 
 
     <li>{{myJ.title}}</li> 
 
    </ul> 
 
    <ul ng-repeat="myN in chilD"> 
 
     <li> 
 
      <h3 style="color:red">{{myN.key}}</h3></li> 
 
     <li>{{myN.title}}</li> 
 
     </ul> 
 
    </div> 
 

 

 

 

 

 
    <script src="js/angular.min.js "></script> 
 
    <script src="js/jquery-1.12.4.min.js "></script> 
 
    <script src="js/bootstrap.min.js "></script> 
 
    <script src="js/npm.js "></script> 
 
    <script src="js/jstree.min.js "></script> 
 
    <script src="tree.js "></script> 
 
</body> 
 

 
</html>

我的一些JSON項目都是家長和一些主題是孩子。 如何做到尊重父母和孩子的規則,並在與nLogn的樹中展示主題?

+2

的可能的複製[角JS像格式樹呈現JSON(http://stackoverflow.com/questions/22168343/angular-js-render-json-in-tree-像格式) –

+0

我不明白「關於父母和孩子的規則」你的'數據'對象只有一個子節點,你能否詳細說明? –

+0

也做一個'角度遞歸指令'或'角度遞歸模板'的網絡搜索。應該找到大量的例子和教程 – charlietfl

回答

0

這就是答案!

var phonecatApp = angular.module('myApp', []) 
 
phonecatApp.controller('myController', function myController($scope, $http) { 
 
    $http.get('https://api.zotero.org/users/475425/collections/9KH9TNSJ/items?format=json') 
 
    .then(function(response) { 
 
     var data = response.data 
 

 
     data = data.filter(function(obj) { 
 
      return true 
 
     }) 
 
     .map(function(obj) { 
 
      return { 
 
      key: obj.key, 
 
      parentItem: obj.data.parentItem, 
 
      title: obj.data.title 
 
      } 
 
     }) 
 
     console.log(data) 
 

 
     $scope.getParentData = function() { 
 
     var log = [] 
 

 
     angular.forEach(data, function(value, key) { 
 
      if (value.parentItem === undefined) { 
 
      this.push(value) 
 
      } 
 
     }, log) 
 
     return log 
 
     console.log(log) 
 
     } 
 

 
     $scope.getChilds = function(p) { 
 
     var log = [] 
 

 
     angular.forEach(data, function(value, key) { 
 
      if (!value.parentItem || value.parentItem !== p.key) { 
 
      return 
 
      } 
 
      this.push(value) 
 
     }, log) 
 
     return log 
 
     console.log(log) 
 
     } 
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html ng-app="myApp"> 
 

 
<head> 
 
    <title>nested tree</title> 
 
</head> 
 

 
<body ng-controller="myController" class="container"> 
 

 
    <ul> 
 
    <li ng-repeat="myPar in getParentData()"> 
 
     <h4>{{myPar.title}}</h4> 
 
     <ul> 
 
     <li ng-repeat="myChild in getChilds(myPar)"> 
 
      <p>{{myChild.title}}</p> 
 
     </li> 
 
     </ul> 
 
    </li> 
 
    </ul> 
 
</body> 
 

 
</html>

0

你可以使用這個偉大的指令:角樹視圖

http://ngmodules.org/modules/angular.treeview

它需要一個JSON這樣

$scope.treedata = 
[ 
    { "label" : "User", "id" : "role1", "children" : [ 
     { "label" : "subUser1", "id" : "role11", "children" : [] }, 
     { "label" : "subUser2", "id" : "role12", "children" : [ 
      { "label" : "subUser2-1", "id" : "role121", "children" : [ 
       { "label" : "subUser2-1-1", "id" : "role1211", "children" : [] }, 
       { "label" : "subUser2-1-2", "id" : "role1212", "children" : [] } 
      ]} 
     ]} 
    ]}, 
    { "label" : "Admin", "id" : "role2", "children" : [] }, 
    { "label" : "Guest", "id" : "role3", "children" : [] } 
]; 

,並顯示它 enter image description here

你需要一點格式化您的數據可能是