简体   繁体   English

通过ng-repeat中的数组对AngularJS进行深度过滤

[英]AngularJS deep filter through array in ng-repeat

My question is looking similar to other questions. 我的问题看起来与其他问题相似。 But it is different. 但这是不同的。 Please take a look of below code. 请看下面的代码。

I want to filter data by an array of objects. 我想按对象数组过滤数据。

Here is the snippet 这是片段

HTML 的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 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",
            }
      ]
}

I am trying to filter by 我正在尝试过滤

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

But am getting error 但是出现错误

thanks in advance :-) 提前致谢 :-)

You need to write nested ng-repeat to solve this problem. 您需要编写嵌套的ng-repeat来解决此问题。

  • outer ng-repeat for the array ledgerData.ledgers and ledgerData.ledgers数组的外部ng-repeat和
  • inner ng-repeat for transaction array in 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> 

Actually I got the solution. 其实我找到了解决方案。

I've googled hardly and got a library for angularjs-filter . 我几乎没有angularjs-filter搜索,并为angularjs-filter库。

Here is the link it is very good plugin for filter dirty works and how to use it. 这是一个非常好的插件,用于filter dirty works以及如何使用它的链接。

How I got success: 我如何获得成功:

html html

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

AngularJS AngularJS

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

that's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM