javascript - How group objects in array from ng-repeat with filter? -


how group objects in array ng-repeat filter ?

i have array objects, , group objects countries.

sample : have result :

free : australia, india, united states   pay : australia not pay : australia, india 

from :

$scope.lists = [{     "id": 1     "field": [         {             country: "australia",             type: "free"         },         {             country: "australia",             type: "pay"         },         {             country: "australia",             type: "not pay"         },         {             country: "india",             type: "free"         },         {             country: "india",             type: "not pay"         },         {             country: "united states",             type: "free"         },     },     {     "id": 2     "field": [         {             country: "australia",             type: "pay"         },         {             country: "india",             type: "free"         },         {             country: "india",             type: "not pay"         }     } ] 

i tried code :

<div ng-repeat="list in lists">      <ul ng-repeat="(key, value) in list.field | groupby: 'type'">       {{ key }}       <li ng-repeat="country in value">         : {{ country }}        </li>     </ul>  </div> 

solved :

i use angular 1.4.9 , angular-filter 0.5.7

<div ng-repeat="list in lists">      <ul ng-repeat="(key, value) in list.field | groupby: 'type'">       {{ key }}       <li ng-repeat="item in value">         : {{ item.country }}        </li>     </ul>  </div> 

i have created plunker .please check it.

i have used angular 1.2.20 , angular-filter.min.js

i have not changes part of html , js. it's working fine me.

js :

var app = angular.module('app', ['angular.filter']);      app.controller('maincontroller', function($scope) {        $scope.lists = [{         "id": 1,         "field": [             {                 country: "australia",                 type: "free"             },             {                 country: "australia",                 type: "pay"             },             {                 country: "australia",                 type: "not pay"             },             {                 country: "india",                 type: "free"             },             {                 country: "india",                 type: "not pay"             },             {                 country: "united states",                 type: "free"             },       ],         },     ]      }); 

html :

 <body ng-controller="maincontroller">       <ul ng-repeat="(key, value) in lists[0].field | groupby: 'type'">   {{ key }}   <li ng-repeat="country in value">     : {{ country }}    </li> </ul>   </body> 

updated html

<div ng-repeat="list in lists">      <ul ng-repeat="(key, value) in list.field | groupby: 'type'">   {{ key }}   <li ng-repeat="country in value">     : {{ country }}    </li> </ul> </div> 

updated plunker


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -