ng-repeat上的条件过滤器

时间:2017-10-09 19:58:42

标签: angularjs

我见过几个关于为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}]

结果是,父[0]将具有子[0]的详细行,父[1]与子[1],父[2]与子[2]和父[3]将具有3个孩子作为细节,因为所有3个孩子都有一个dl_seq:4,而父[4]是一个dl而且它是seq它是4个

1 个答案:

答案 0 :(得分:0)

尝试在控制器中使用过滤功能:

function itemsFilter(parent) {
    return function(child) {
        return (parent.Type === 'PU' ? child.pu_seq: child.dl_seq) === parent.seq
    };
}

然后过滤:

<tr ng-repeat="child in item.collection | filter: itemsFilter(parent)>