Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an ng-repeat loop returning a list of objects which represent calendar events. I'd like to be able to orderBy date but it looks like first I need to push the returned objects into a single array.

Here is my angular view:

<div class="calendar">

<p>{{cal}}</p>
<div ng-repeat="items in cal">

        <a href="/events/{{items.day}}">
          <article class="eventslist">
           <div class="numberedDate">
               <h3>{{items.day}}</h3>
            </div>
            <div class="calInfo">
            <h5>{{items.title}}</h5>
               <p>{{items}}&nbsp;<a>more</a></p>
            </div>
           </article>


      </div><!-- ng-repeat items -->
</div><!-- calendar -->

items is currently returning:

{"day":"21","title":"ok","summary":"ok","description":"ok","_id":"53ee9f0fc6aed109c6d33cfd"}
{"day":"2","title":"ok","summary":"ok","description":"ok","_id":"53ee9f038782309c6d892"}
{"day":"27","title":"ok","summary":"ok","description":"ok","_id":"533240fc6ae32433chd"}

Is there a way to wrap these returned objects into an array [] so that they could have an orderBy 'day' called on them?

share|improve this question
1  
What are they right in right now? –  tymeJV Aug 19 at 13:47
    
They are currently being pulled out of a parent object and being returned one by one as their own objects. So these objects were originally stored as objects of another object. –  byrdr Aug 19 at 13:55
1  
items is already an array, ng-repeat would not work otherwise. orderby:day on it should work, but you need to cast the value to Integer to get expected order. –  Sergiu Paraschiv Aug 19 at 14:03

1 Answer 1

up vote 1 down vote accepted

If you just need to get the items into an array, the below loop will do it:

var arr = [];
angular.forEach(items, function (item) {
    arr.push(item);
});

I know your data has a complicated structure, so you may need a couple of loops to pull out the objects you need and flatten the array.

Once your data is structured, Then the code below will work. It will handle an array or an object as long as you get the ng-repeat parameters right. (I've tested it on array):

myApp.filter('orderByDayNumber', function () {
    return function(items, field, reverse) {
        var filtered = [];
        angular.forEach(items, function (item) {
            filtered.push(item);
        });
        filtered.sort(function (a, b) {
            return (parseInt(a[field]) > parseInt(b[field]) ? 1 : -1);
        });
        return filtered;
    };
});

Note the parseInt call in the sort function. I have actually seen a function passed in as a parameter inside the ng-repeat but you can investigate that later if you like.

Anyway, Here is a working demo jsFiddle

share|improve this answer
    
Great! Thanks so much for your help and explanation. –  byrdr Aug 19 at 15:25
    
Would this be the correct way to use the foreach? var arr = []; angular.forEach($scope.calendar, function (item) { arr.push(item); }); $scope.newitem = arr; –  byrdr Aug 19 at 17:29
    
Yes, I think that looks good. Did you get it all working? –  CaspNZ Aug 19 at 21:09
    
I haven't been able to get it working right yet. It's probably due to an error on my part somewhere though. –  byrdr Aug 20 at 0:40
    
If you put up a plnkr with the actual data that you are getting, I could have a go at getting it working –  CaspNZ Aug 20 at 0:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.