【问题标题】:Show JSON as nested tree with AngularJS使用 AngularJS 将 JSON 显示为嵌套树
【发布时间】:2016-05-29 06:48:18
【问题描述】:

我有这个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>

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

【问题讨论】:

  • 我不明白“关于父子规则”你的data对象只有一个子节点,你能详细说明一下吗?
  • 同时在网络上搜索angular recursive directive 或angular recursive template。应该找到许多示例和教程

标签: javascript angularjs json


【解决方案1】:

你可以使用这个很棒的指令:angular treeview

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" : [] }
];   

并显示它

您可能需要对数据进行一些格式化

【讨论】:

    【解决方案2】:

    这就是答案!!!!

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多