0

My client needs an angular js app, the app is based on an api, of an app that's already running. one of the apis, returns a json structured like this:

{  
   "groups":{  
      "60":{  
         "name":"History"
      },
      "74":{  
         "name":"Discover our offers"
      },
      "109":{  
         "name":"Relaxing"
      }
   }
}

so we fetched the data on a controller this way:

  $http({method: 'GET', url: restUrl}).
      then(function(response)
      {
          $scope.poi_groups = response.data.groups;
      });

and display it on the view:

<ul class="content-menu">
    <li ng-repeat="(key, value) in poi_groups">
        <div><a ng-href="/poi/data/{{ key }}">{{ value.name }}</a></div>
    </li>
</ul>

What I've been struggling with is ordering the items by name, right now its being displayed exactly the way its returned on the json. How can I do something like:

(...)
<li ng-repeat="(key, value) in poi_groups | orderBy: value.name">
(...)
user3531149
  • 1,519
  • 3
  • 30
  • 48

2 Answers2

1

This structure isn't appropriate for angularjs, either :

  1. Do it yourself in the .then method
  2. Write your own filter
  3. Map your object in a new one in the .then method to make it look like this :

    [{ value:'60', name:'History' }, { .... }]

Then you can use angular filtering.

Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • ended up doing this for(var index in $scope.poi_groups) { map_groups_to_array.push( { index: index, name: $scope.poi_groups[index].name }); }; – user3531149 Feb 08 '16 at 10:00
  • 1
    This solution is the one that i use because i leave the request part in angular services that take the responsability of call the http request and format data as the controllers need. Transform timestamp to date for instance to not have any problem with all date widgets, or just transform the whole object because his structure does not fit for angular. – Walfrat Feb 08 '16 at 10:12
0

use ngRepeat orderBy

Use filter like orderBy: 'name'

 <li ng-repeat="(key, value) in poi_groups| orderBy:'name'">

and

 <li ng-repeat="(key, value) in poi_groups| orderBy:'name': -1">

for DESC

if you make plnk we can check it out

I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176