AngularJS深度过滤ng-repeat中的数组

时间:2015-10-29 09:04:00

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

我的问题与其他问题类似。但它是不同的。 请看下面的代码。

我想通过一组对象过滤数据。

这是片段

HTML

<div
ng-repeat="(key, value) in ledgerData.ledgers track by $index"
ledger-pop 
index="$index"
ftype="ftypeUpdate"
itemdata="value" 
acntlist="fltAccntList"
class='drEntryForm_{{$index}} pr'
name='drEntryForm_{{$index}}'
update-ledger="updateEntry(entry)"
novalidate
>
</div>

JS

$scope.ledgerDataata = {
      "ForwardedBalance": {
      "amount": 0,
      "type": "CREDIT"
      },
      "creditTotal": 4008,
      "debitTotal": 4008,
      "balance": {
      "amount": 4008,
      "type": "CREDIT"
      },
      "ledgers": [
            {
              "transactions": [
                {
                  "particular": {
                    "name": "Sarfaraz",
                    "uniqueName": "temp"
                  },
                  "amount": 1001,
                  "type": "DEBIT"
                }
              ],
              "description": "testing",
              "tag": "testing"
            },
            {
              "transactions": [
                {
                  "particular": {
                    "name": "frnd",
                    "uniqueName": "frndafafaf14453422453110l26ow"
                  },
                  "amount": 2001,
                  "type": "CREDIT"
                },
                {
                  "particular": {
                    "name": "Rahul",
                    "uniqueName": "cake"
                  },
                  "amount": 3001,
                  "type": "DEBIT"
                }
              ],
              "description": "testing",
              "tag": "testing",
            }
      ]
}

我正在尝试按

过滤
ng-repeat="(key, value) in ledgerData.ledgers track by $index | filter:{transactions[0]:{type: 'DEBIT'}}"

但是收到错误

提前感谢: - )

2 个答案:

答案 0 :(得分:2)

您需要编写嵌套的ng-repeat来解决此问题。

  • 外部ng-repeat用于数组ledgerData.ledgers和
  • ledgerData.ledgers中的事务数组的内部ng-repeat

        <div ng-repeat="(keyOne, ledger) in ledgerData.ledgers track by $index">
                   {{ledger.description}}
             <div ng-repeat="(keyTwo, transaction) in ledger.transactions | filter:{type:'DEBIT'}">
                      {{transaction.type}} 
             </div>
        </div>

答案 1 :(得分:0)

实际上我得到了解决方案。

我几乎没有谷歌搜索,并为angularjs-filter建立了一个图书馆。

Here is the link这是filter dirty works非常好的插件以及如何使用它。

我是如何取得成功的:

<强> HTML

ng-repeat="(key, value) in ledgerData.ledgers | pick: debitOnly track by $index"

<强> AngularJS

$scope.debitOnly = (ledger) ->
  'DEBIT' == ledger.transactions[0].type

是的。