【发布时间】:2017-10-09 19:58:42
【问题描述】:
我看到了几个关于向 ng-repeat 过滤器添加条件的问题,但这些选项都没有对我有用
我有以下自定义过滤器
app.filter('customFilter', function () {
return function (collection, type, seq) {
return !collection ? [] : collection.filter(function (item) {
return (type === 'DL' && item.del_seq === seq) || (type === 'PU' && item.pu_seq === seq)
});
}
});
这是我的指令
<tr ng-repear-start="parent in parentCollection">
<td> Do stuff with parent collection items info </td>
</tr>
<tr ng-repeat-end="">
<td colspan="100">
<table>
<tr ng-repeat="child in childCollection | customFilter: parent.Type:parent.seq">
<td>Do stuff with child info</td>
</tr>
</table>
</td>
</tr>
这很完美,但我想知道,是否有办法使用简单的过滤器来做到这一点?
我尝试了以下操作
<tr ng-repeat="child in childCollection | filter: (parent.Type === 'PU' ? child.pu_seq: child.dl_seq) === parent.seq">
但它没有用。认为没有自定义也是可能的吗?
编辑:根据要求,这是一个示例 JSON,并且调整了 html 以获得更好的想法
parentCollection = [{seq: 1, type:'PU'},{seq: 2, type:'PU'},{seq: 3, type:'PU'},{seq: 4, type:'DL'}];
childCollection = [{pu_seq: 1, dl_seq:4},{pu_seq: 2, dl_seq:4},{pu_seq: 3, dl_seq:4}]
结果将是,parent[0] 将有一个包含 child[0] 的详细信息行,parent[1] 与 child[1],parent[2] 与 child[2],并且 parent[3] 将具有3 个孩子作为细节,因为所有 3 个都有一个 dl_seq:4,而 parent[4] 是一个 dl,它的 seq 它是 4
【问题讨论】:
-
在这里发布你的 json
-
@Sajeetharan json 添加
标签: angularjs