javascripthtmlangularjsangularjs-ng-repeatangularjs-filter

Sorting Array of JSON Objects with AngularJS


I'm trying to sort an "entry" resource by months between different tables.

My data is as follows :

[
    {
        id: 1,
        date: "2011-04-01",
        text: "My first entry"
    },
    {
        id: 2,
        date: "2011-06-07",
        text: "My second entry"
    },
    ...
]

And I want to display the entries in different tables based on the month.

I've made a "months" array and tried this :

<div ng-repeat="month in months">
    <h3>{{month}}</h3>
    <ul ng-repeat="entry in entries">
        {{entry.text}}
    </ul>
</div>

Of course, this puts every entry in every month. How can I only display the entries that belong in each month ?


Solution

  • You could use the "filter" filter:

    <div ng-repeat="month in months">
        <h3>{{month}}</h3>
        <ul ng-repeat="entry in entries | filter:{date:month}">
            {{entry.text}}
        </ul>
    </div>
    

    You'll need to do some manipulation of the date property to compare to the month.