0

I die json Daten wie unten haben [{ "id": 1, "address": "MG Road", "country": INDIA, "state": AP, "city": VIJ }, { "id": 2, "address": "Miyapur", "country": INDIA, "state": TS, "city": HYD }, { "id": 3, "address": "Autonagar", "country": INDIA, "state": AP, "city": VIJ }, { "id": 4, "address": "Kukatpalli", "country": INDIA, "state": TS, "city": HYD }, { "id": 5, "address": "Koti", "country": INDIA, "state": TS, "city": HYD } ]GroupBy mit mehreren Attributen in Winkelfilter

I wie unten Format angezeigt werden soll

IND, TS, HYD
          Miyapur, Koti, Kukatpalli
IND, AP, VIJ,
          MG Road, Autonagar

Zu diesen im wie unter Verwendung von groupBy filtern, aber ich war mein Code nicht in der Lage dieser Werte

hier zu Gruppe

 <div class="location-container"> 
      <label ng-repeat="(key,value) in locationmodel | groupBy: '[country,state,city]'">{{key}} 
      </label> 
      <span ng-repeat="location in value">{{location.address}}</span> 
     </div> 

aber mit obigem Code war ich unfähig zu bekommen, was ich erwartet habe. Bitte helfen Sie mir bei der Lösung dieses Problems.

Vielen Dank im Voraus

Antwort

0

Wenn Sie mögen, können versuchen, diese eine Ausgabe

<!DOCTYPE html> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script> 
    var myApp = angular.module('myApp', []); 
    myApp.controller('myAppCtrl', function ($scope) { 
    var locationmodel = [{ 
     "id" : 1, 
     "address" : "MG Road", 
     "country" : "INDIA", 
     "state" : "AP", 
     "city" : "VIJ" 
    }, { 
     "id" : 2, 
     "address" : "Miyapur", 
     "country" : "INDIA", 
     "state" : "TS", 
     "city" : "HYD" 
    }, { 
     "id" : 3, 
     "address" : "Autonagar", 
     "country" : "INDIA", 
     "state" : "AP", 
     "city" : "VIJ" 
    }, { 
     "id" : 4, 
     "address" : "Kukatpalli", 
     "country" : "INDIA", 
     "state" : "TS", 
     "city" : "HYD" 
    }, { 
     "id" : 5, 
     "address" : "Koti", 
     "country" : "INDIA", 
     "state" : "TS", 
     "city" : "HYD" 
    } 
    ]; 
    var resultObj = {}; 
    for (var i = 0; i < locationmodel.length; i++) { 
    if (resultObj[locationmodel[i].country + ',' + locationmodel[i].state + ',' + locationmodel[i].city]) 
     resultObj[locationmodel[i].country + ',' + locationmodel[i].state + ',' + locationmodel[i].city].address.push(locationmodel[i].address); 
    else 
     resultObj[locationmodel[i].country + ',' + locationmodel[i].state + ',' + locationmodel[i].city] = { 
     address : [locationmodel[i].address] 
     }; 
    } 

$scope.result=resultObj; 
    }); 
</script> 
<body ng-app="myApp" ng-controller='myAppCtrl'> 
<div class="location-container" ng-repeat="(key,value) in result"> 
    <label >{{key}}</label><br> 
    <span>{{value.address.join()}}</span> 
</div> 
</body> 

+0

Arbeiten perfekt :) Vielen Dank – user2451830

0

Hier zu bekommen, ist eine Lösung: https://jsfiddle.net/mqt0xjjc/

HTML:

<div ng-app="myApp"> 
<div ng-controller="Main"> 
    <div ng-repeat=" (groupedBy, groupedItems) in locationmodelGrouped"> 
     <b>{{groupedBy}}</b> 
     <li ng-repeat="item in groupedItems">{{item.address}}</li>   
    </div> 
</div> 
</div> 

JS:

angular.module('myApp', []).controller('Main', 
function($scope) { 
    function groupBy(items, groupByAttrs) { 
     const retVal = items.reduce(
      function (sum, item) { 
      const key = groupByAttrs.map(attr => item[attr]).join(','); 
      sum[key] = sum[key] || []; 
      sum[key].push(item); 
      return sum; 
      }, 
      {} 
     ); 
     return retVal; 
    }; 

    $scope.$watch('locationmodel', 
     function() { 
     $scope.locationmodelGrouped = groupBy($scope.locationmodel, ['country','state','city']) 
     } 
    ) 

$scope.locationmodel = [{ 
    "id": 1, 
    "address": "MG Road", 
    "country": 'INDIA', 
    "state": 'AP', 
    "city": 'VIJ' 
}, 
{ 
    "id": 2, 
    "address": "Miyapur", 
    "country": 'INDIA', 
    "state": 'TS', 
    "city": 'HYD' 
}, 
{ 
    "id": 3, 
    "address": "Autonagar", 
    "country": 'INDIA', 
    "state": 'AP', 
    "city": 'VIJ' 
}, 
{ 
    "id": 4, 
    "address": "Kukatpalli", 
    "country": 'INDIA', 
    "state": 'TS', 
    "city": 'HYD' 
}, 
{ 
    "id": 5, 
    "address": "Koti", 
    "country": 'INDIA', 
    "state": 'TS', 
    "city": 'HYD' 
} 
]; 

}); 
Verwandte Themen