10

I just took the simplest demo from http://docs.angularjs.org/api/ng.filter:orderBy and just change the value of age to have different number of digit. It stop working as it expected. Its ordering like "string" not as "integer" value. How should i change it so that it order by age like integer value?

Plunkr demo is here http://plnkr.co/edit/pzgiIYrki7jUZdVaTMt9?p=preview

Codes are:

function Ctrl($scope) {
  $scope.friends =
      [{name:'John', phone:'555-1212', age:'2352345'},
       {name:'Mary', phone:'555-9876', age:'4235243'},
       {name:'Mike', phone:'555-4321', age:'241'},
       {name:'Adam', phone:'555-5678', age:'34325'},
       {name:'Julie', phone:'555-8765', age:'1234'}]
  $scope.predicate = '-age';
}


<!doctype html>
<html ng-app="App">
  <head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
  <body>

<div ng-controller="Ctrl">
  <pre>Sorting predicate = {{predicate}}; reverse = {{reverse}}</pre>
  <hr/>
  [ <a href="" ng-click="predicate=''">unsorted</a> ]
  <table class="friend">
    <tr>
      <th><a href="" ng-click="predicate = 'name'; reverse=false">Name</a>
          (<a href="" ng-click="predicate = '-name'; reverse=false">^</a>)</th>
      <th><a href="" ng-click="predicate = 'phone'; reverse=!reverse">Phone Number</a></th>
      <th><a href="" ng-click="predicate = 'age'; reverse=!reverse">Age</a></th>
    </tr>
    <tr ng-repeat="friend in friends | orderBy:predicate:reverse">
      <td>{{friend.name}}</td>
      <td>{{friend.phone}}</td>
      <td>{{friend.age}}</td>
    </tr>
  </table>
</div>


  </body>
</html>
4

6 回答 6

19

您必须将年龄转换为类型 Number 才能orderBy正常工作。

添加到您的控制器以将字符串年龄排序为浮点数:

   angular.forEach($scope.friends, function (friend) {
    friend.age = parseFloat(friend.age);
   });

它应该工作,

PLunker

于 2013-10-04T07:29:54.853 回答
11

我知道这个答案有点晚了,但是在最新版本的 Angular 中,您可以使用函数作为 orderBy 的谓词(https://docs.angularjs.org/api/ng/filter/orderBy)。在这种情况下,向控制器添加一个类似 next 的函数

$scope.orderByFunction = function(friend){
    return parseInt(friend.age);
};

并更改 orderBy 过滤器中的谓词

ng-repeat="friend in friends | orderBy:orderByFunction:reverse"

这会成功的!

于 2015-03-05T02:09:46.320 回答
1

您只需要删除 age 变量中的引号,因为它是整数,而不是字符串:

  $scope.friends =
      [{name:'John', phone:'555-1212', age:2352345},
       {name:'Mary', phone:'555-9876', age:4235243},
       {name:'Mike', phone:'555-4321', age:241},
       {name:'Adam', phone:'555-5678', age:34325},
       {name:'Julie', phone:'555-8765', age:1234}]
于 2014-10-03T23:33:21.993 回答
0

将谓词更改为以下内容也将起作用:

<th><a href="" ng-click="predicate = '1*age'; reverse=!reverse">Age</a></th>
于 2017-03-01T22:30:37.500 回答
0

AngularJs 具有对值进行排序的内置功能。但是在对浮点值进行排序之前,我们需要使用 parseFloat() javavscript 函数将值转换为浮点数。

http://hubpages.com/technology/How-to-sort-table-content-filter-table-data-and-add-pagination-using-Angular-JS

于 2015-11-20T23:29:03.100 回答
-1

在将数组转换为 json 时添加 JSON_NUMERIC_CHECK 代码。json_encode($anyarray,JSON_NUMERIC_CHECK);

于 2016-10-27T12:27:57.877 回答