2016-05-13 87 views
2

我是角度新人。所以我正在阅读写作只是为了完成我的工作。这样我会尝试,但不知道我是否在正确的方向?这里是我的示例代码如何使用ng-repeat以表格格式显示分层数据

<ul> 
    <li ng-repeat="parent in items">{{parent.pitem}} 
     <ul> 
      <li ng-repeat="child in parent.citems">{{child.pitem }}</li> 
     </ul> 
    </li> 
</ul> 

var app = angular.module('myapp', []); 

app.controller('MyCtrl', function ($scope) { 
    $scope.items = [{ "pitem": "Coffee", "citems": "" }, { "pitem": "Tea", "citems": [{ "pitem": "Black tea", "citems": "" }, { "pitem": "Green tea", "citems": "" }] }, { "pitem": "Milk", "citems": "" }] 
}); 

enter image description here

我其实我需要表现出员工层级像经理及其下属。

我也一样样的话题由@zsong https://stackoverflow.com/a/18295177/6188148

所写这也所以请帮我生成使用ul,li和最终用户应该能够展开折叠行太NG重复表格输出。请看看这个网址http://maxazan.github.io/jquery-treegrid/examples/example-bootstrap-3.html我想要类似的输出,但用ng-repeat。

看到这个url http://www.lidorsystems.com/support/articles/angularjs/treegrid/tree-grid-filter.aspx然后可以理解我想用ng-repeat构建什么样的输出。

请指导和建议。

+0

为什么负,当我展示我的努力。 – Mou

+0

你必须去递归,所以你需要用ng-repeat注​​入DOM,而不是在DOM中写下你的ng-repeat作为声明 – Raffaeu

+0

为什么不使用角度树视图。看到这个示例http://jsfiddle.net/eu81273/8LWUc/18/ –

回答

3

此的jsfiddle https://jsfiddle.net/benfosterdev/NP7P5/显示你正在寻找什么:

递归

<div ng-app="app" ng-controller='AppCtrl'> 
    <script type="text/ng-template" id="categoryTree"> 
     {{ category.title }} 
     <ul ng-if="category.categories"> 
      <li ng-repeat="category in category.categories" ng-include="'categoryTree'">   
      </li> 
     </ul> 
    </script> 
    <ul> 
     <li ng-repeat="category in categories" ng-include="'categoryTree'"></li> 
    </ul>  
</div> 

JSON对象

$scope.categories = [ 
    { 
    title: 'Computers', 
    categories: [ 
     { 
     title: 'Laptops', 
     categories: [ 
      { 
      title: 'Ultrabooks' 
      }, 
      { 
      title: 'Macbooks'    
      } 
     ] 
     }, 

     { 
     title: 'Desktops' 
     }, 

     { 
     title: 'Tablets', 
     categories: [ 
      { 
      title: 'Apple' 
      }, 
      { 
      title: 'Android' 
      } 
     ]   
     } 
    ] 
    }, 

    { 
    title: 'Printers' 
    } 

]; 

}); 
+0

你的代码非常特别。你的代码就像我一样给出相同的输出。我希望子数据不应该显示,而不是每个子数据之前会有折叠图标,当用户单击折叠图标,然后展开图标来和子数据将显示,用户可以再次单击展开图标以使数据崩溃像树视图。 – Mou

+0

我想知道如何使用ng-repeat生成类似于树状网格的输出。 – Mou

+0

看到这个网址http://www.lidorsystems.com/support/articles/angularjs/treegrid/tree-grid-filter.aspx然后可以理解我想用ng-repeat构建什么样的输出。 – Mou