使用AngularJS将JSON显示为嵌套树

时间:2016-05-29 06:48:19

标签: javascript angularjs json

我有这个JSON,我需要将这个节点的三个节点显示为带有角度js的树:data.key& data.parentItem& data.title。

这是我的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>
&#13;
&#13;
&#13;

我的一些JSON项目是父项,一些主题是孩子。 如何针对父子规则进行操作并使用nLogn在树中显示主题?

2 个答案:

答案 0 :(得分:1)

你可以使用这个伟大的指令:angular treeview

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

它需要像这样的json

<div class="panel">
  {{question.text}}
</div>
<choice *ngFor="let choice of question.choices" [choice]="choice">

并显示它 enter image description here

您需要格式化一些数据

答案 1 :(得分:0)

这就是答案!!!!

&#13;
&#13;
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)
      }
    })
})
&#13;
<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>
&#13;
&#13;
&#13;