OrderBy谓词

时间:2014-11-14 10:49:19

标签: angularjs angularjs-filter angularjs-orderby

我有这段代码:

   var app = angular.module('app', []);
   app.controller('RadioCtrl', ['$scope', '$http',
     function($scope, $http) {
       $http.jsonp('http://www.filltext.com/?callback=JSON_CALLBACK&rows=5&fname={firstName}&lname={lastName}&id={index}').success(function(data) {
         $scope.people = data;
       })
     }
   ]);
<!DOCTYPE html>
<html ng-app="app">

<head>
  <meta charset="utf-8">
  <title>Radio button</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
</head>

<body>
  <section ng-controller="RadioCtrl">
    <input type="text" ng-model="pepek.fname">
    <br>Ascended
    <input type="radio" ng-model="direction" checked>Descended
    <input type="radio" ng-model="direction" value="reverse">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>First Name</th>
          <th>Second name</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="person in people| filter:pepek |orderBy:person.id">
          <td>{{person.id}}</td>
          <td>{{person.fname}}</td>
          <td>{{person.lname}}</td>
        </tr>
      </tbody>
    </table>

  </section>
</body>

</html>

我想点击单选按钮Ascending或Descending,我的表格将按ID排列。 即使我尝试简单的orderBy:idorderBy:-idorderBy:person.idorderBy:id:reverseorderBy:id:direction,但这些情况并未按ID排列,它始终会降序我不知道为什么。 提前谢谢你:)

2 个答案:

答案 0 :(得分:1)

ng-repeat表达并不完全正确。应该是这样的:

ng-repeat="person in people| filter:pepek | orderBy:'id':reverse"

并且单选按钮声明需要如下所示:

<input type="radio" ng-model="reverse" ng-value="false">Descended
<input type="radio" ng-model="reverse" ng-value="true">

&#13;
&#13;
var app = angular.module('app', []);
   app.controller('RadioCtrl', ['$scope', '$http',
     function($scope, $http) {
       
       $scope.reverse = false;
       
       $http.jsonp('http://www.filltext.com/?callback=JSON_CALLBACK&rows=5&fname={firstName}&lname={lastName}&id={index}').success(function(data) {
         $scope.people = data;
       })
     }
   ]);
&#13;
<!DOCTYPE html>
<html ng-app="app">

<head>
  <meta charset="utf-8">
  <title>Radio button</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
</head>

<body>
  <section ng-controller="RadioCtrl">
    <input type="text" ng-model="pepek.fname">
    <br>Ascended
    <input type="radio" ng-model="reverse" ng-value="false">Descended
    <input type="radio" ng-model="reverse" ng-value="true">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>First Name</th>
          <th>Second name</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="person in people| filter:pepek |orderBy:'id':reverse">
          <td>{{person.id}}</td>
          <td>{{person.fname}}</td>
          <td>{{person.lname}}</td>
        </tr>
      </tbody>
    </table>

  </section>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是因为您的ID可能存储为String值而不是整数。试试这个:

<tr ng-repeat="person in people | orderBy:parseFloat(person.id)">