espenk
espenk

Reputation: 585

angular orderBy not working

I created this codepen for the problem http://codepen.io/anon/pen/JaAtm

The problem is that when I'm selecting 2014 as the filter in the select, the order of the month are not working.

<html ng-app>
<head></head>
<body>
<div ng-controller="cor">
  <select ng-model="s">
    <option value=2013>2013</option>
    <option value=2014>2014</option>
  </select>
  <table>
    <tr>
      <th>Year</th>
      <th>Month</th>
      <th>Days</th>
    </tr>
    <tr ng-repeat="item in items | orderBy:Month | filter:s">
      <td>{{item.year}}</td>
      <td>{{item.Month}}</td>
      <td>{{item.NumberOfDays}}</td>
  </table>
</div>

function cor($scope) { $scope.items = [{"NumberOfDays":"41","Month":"1","MonthName":"Januar","year":"2013"}.....

Upvotes: 0

Views: 184

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

Even when not selecting anything, it doesn't order correctly. The orderBy should be:

orderBy:'Month'

because you want to order your dates by the property named 'Month', and not by their property named by the value of $scope.Month.

The next problem is that you stored your months as strings instead of storing them as numbers, so the lexicographic order is used instead of the numeric order.

Upvotes: 3

Related Questions