如何根据内部数组值过滤外部数组记录

时间:2015-04-24 08:52:22

标签: angularjs angularjs-ng-repeat angularjs-filter

我在另一个对象中有一个数组对象。所以,我的html中有两个嵌套的ng-repeat。如果ACTION未“更改”,我试图隐藏标签数据 对于前在下面的示例中,最后一行“Changed L3”不应显示。

我的范围数据

 $scope.groups =
    [{
      "label": "L1",
      "tabledata": [{
                      "FIELDNAME": "C1 Entity",
                      "SCREEN": "A",
                      "ORIGINALVALUE": "NA",
                      "ACTION": "Changed",
                      "NEWVALUE": "Something",
                      "TYPEACT": "RB"

                   },
                   {
                          "FIELDNAME": "C2 Entity",
                          "SCREEN": "A",
                          "ORIGINALVALUE": "NA",
                          "ACTION": "Changed",
                          "NEWVALUE": "SSSS",
                          "TYPEACT": "RB"

                       }
                      ]

    },
    {
      "label": "L2",
      "tabledata": [{
                      "FIELDNAME": "B1 Entity",
                      "SCREEN": "B",
                      "ORIGINALVALUE": "NA",
                      "ACTION": "Changed",
                      "NEWVALUE": "Something",
                      "TYPEACT": "RB"

                   },
                   {
                      "FIELDNAME": "B2 Entity",
                      "SCREEN": "B",
                      "ORIGINALVALUE": "NA",
                      "ACTION": "Changed",
                      "NEWVALUE": "Something",
                      "TYPEACT": "RB"

                   }
                   ]

    },

     {
      "label": "L3",
      "tabledata": [{
                      "FIELDNAME": "A Entity",
                      "SCREEN": "C",
                      "ORIGINALVALUE": "NA",
                      "ACTION": "Added",
                      "NEWVALUE": "Something",
                      "TYPEACT": "RB"

                   }]

    }]

我的HTML

<div ng-repeat="group in groups" >
        <p><b>{{ "Changed " + group.label }}</b>
                </p>
        <table class="table table-bordered" >
            <tbody >
                <tr >
                    <th width ="20%"><b ng-bind="column1" ></b></th>
                    <th width ="40%"><b>Previous value </b></th>
                    <th width ="40%"><b ng-bind="column3" ></b> </th>
                    <th width ="10%"><b>Action </b></th>
                </tr>
                <!-- BEGIN: Inner ngRepeat. -->
                <tr ng-repeat="endorsedata in group.tabledata | filter :{ACTION :  'Changed'}  | orderBy:'FIELDNAME' " >
                    <td ng-init ="showRow = 1">{{ endorsedata.FIELDNAME }}</td>
                    <td>{{ endorsedata.ORIGINALVALUE }}</td>
                    <td>{{ endorsedata.NEWVALUE }}</td>
                    <!-- <td>{{ endorsedata.ACTION }}</td> -->
                </tr>
                <!-- END: Inner ngRepeat. -->
            </tbody>
        </table>
    </div>

如果在ACTION属性中存在“已更改”以外的值,我想隐藏标签。这怎么能实现?

Plunker Here

编辑:更新了Plunker Link。

1 个答案:

答案 0 :(得分:1)

我们接下来,这应该做你想做的事:plunker

基本上,创建了一个与角度$filter具有相同重载的过滤器。然后在每个tabledata里面我使用过滤器选项。

如果过滤后的数组与原始数组不同,那么我们可以放心地说找到了一些东西。您可以轻松修改它以满足您的确切需求。

该示例显示了如何通过子数组过滤最顶层的数组。