【问题标题】:Conditional Filter on ng-repeatng-repeat 上的条件过滤器
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 好吧,我想我已经用自定义过滤器做了一些事情。我想知道的是,如果我可以用一个简单的过滤器语句实现相同的结果,而不是依赖于函数或自定义过滤器,但是感谢您的提示,看看我是否可以在其他地方使用
猜你喜欢
  • 2017-02-21
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
相关资源
最近更新 更多