角度 - 表格中的多重ng重复

时间:2015-03-16 10:38:52

标签: angularjs angularjs-ng-repeat

[编辑:解决但我认为有更好的方法,如果你找到它,我仍然感兴趣]

我有一份银行名单。 每个银行都包含一个帐户列表。 每个帐户都包含一系列操作。

我想在表格中显示所有操作。

这是银行名单的样子:

$scope.banks = [
    {
        id: 1,
        name: 'bank_1',
        accounts: [
            {
                id: 1,
                name: 'account_1',
                operations: [
                    {id: 1, name:'operation_1', price:100},
                    {id: 2, name:'operation_2', price:200},
                    {id: 3, name:'operation_3', price:300},
                    {id: 4, name:'operation_4', price:400}
                ]
            },
            {
                id: 2,
                name: 'account_2',
                operations: [
                    {id: 5, name:'operation_5', price:100},
                    {id: 6, name:'operation_6', price:200},
                    {id: 7, name:'operation_7', price:300},
                    {id: 8, name:'operation_8', price:400}
                ]
            }
        ]
    },
    {
        id: 2,
        name: 'bank_2',
        accounts: [
            {
                id: 3,
                name: 'account_3',
                operations: [
                    {id: 9, name:'operation_9', price:100},
                    {id: 10, name:'operation_10', price:200},
                    {id: 11, name:'operation_11', price:300},
                    {id: 12, name:'operation_12', price:400}
                ]
            },
            {
                id: 4,
                name: 'account_4',
                operations: [
                    {id: 13, name:'operation_13', price:100},
                    {id: 14, name:'operation_14', price:200},
                    {id: 15, name:'operation_15', price:300},
                    {id: 16, name:'operation_16', price:400}
                ]
            }
        ]
    }
];

为了用Angular正确显示,我想做出类似的东西:

<table>
    <tr>
        <th>Banque</th>
        <th>Account</th>
        <th>ID OP</th>
        <th>Name</th>
        <th>Price</th>
    </tr>

    <div ng-repeat="bank in banks">
        <div ng-repeat="account in bank.accounts">
            <div ng-repeat="operation in account.operations">
                <tr>
                    <td>{{banque.name}}</td>
                    <td>{{account.name}}</td>
                    <td>{{operation.id}}</td>
                    <td>{{operation.name}}</td>
                    <td>{{operation.price}}</td>
                </tr>
            </div>
        </div>
    </div>

</table>

但我知道打破桌子不正确&gt; tr> TD。

经过一些研究,我想我可能不得不使用ng-repeat-start,ng-repeat-end指令,但我不明白怎么做,我甚至不确定这是好事。

如果你有任何想法如何解决,我很高兴听到你的解决方案。

2 个答案:

答案 0 :(得分:5)

这可以通过创建将返回操作列表的自定义过滤器轻松实现。

过滤

.filter('filterData', function(){
  return function(data, firstParam, secondParam){
    var returnData = [];
    angular.forEach(data, function(value, index){
      angular.forEach(value[firstParam], function(val, ind){
        angular.forEach(val[secondParam], function(v, i){
          returnData.push(v)
        });
      });
    });
    return returnData;
  }
});

<强>标记

<table>
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Price</th>
  </tr>
  <tr ng-repeat="operation in banks| filterData: 'accounts': 'operations'">
    <td>{{operation.id}}</td>
    <td>{{operation.name}}</td>
    <td>{{operation.price}}</td>
  </tr>

</table>

Working Plunkr这里

希望这可以帮助你,谢谢。

答案 1 :(得分:0)

你救了我的一天。

我增强了你的解决方案,因为filterData函数运行了两次(对于$ scope.banks数组的每个元素)。

更喜欢tr_ngrepeat的tbody容器中的ng-init。

<table>
 <thead>
   <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Price</th>
  </tr>
 </thead>
 <tbody ng-init="operationlist=(banks|filterData: 'accounts': 'operations')">
  <tr ng-repeat="operation in operationlist">
    <td>{{operation.id}}</td>
    <td>{{operation.name}}</td>
    <td>{{operation.price}}</td>
  </tr>
 </tbody>
</table>