gpt4 book ai didi

angularjs - Angular - 表中多次重复

转载 作者:行者123 更新时间:2023-12-03 08:07:38 28 4
gpt4 key购买 nike

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

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

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

以下是银行列表:

$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>

但我知道打破 table > tr > td 是不正确的。

经过一些研究,我认为我可能必须使用 ng-repeat-start、ng-repeat-end 指令,但我不明白如何使用,我什至不确定这是一件好事。

如果您知道如何解决该问题,我很高兴听到您的解决方案。

最佳答案

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

过滤

.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这里

希望对您有帮助,谢谢。

关于angularjs - Angular - 表中多次重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074747/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com