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">
(...)
3
  • Try to add quotes: orderBy: 'value.name' Commented Feb 8, 2016 at 9:20
  • 1
    objects have no order and orderBy is for arrays as per docs. Will need to map to array Commented Feb 8, 2016 at 9:26
  • related stackoverflow.com/questions/25375006/… Commented Feb 8, 2016 at 9:34

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ended up doing this for(var index in $scope.poi_groups) { map_groups_to_array.push( { index: index, name: $scope.poi_groups[index].name }); };
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.
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

2 Comments

for some reason the filter does nothing
@user3531149 because data is not an array and needs to be mapped to array in controller or custom filter to be able to be sorted. Docs for orderBy clearly state it is for array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.