AngularJS orderby integer field not working properly

后端 未结 6 1304
無奈伤痛
無奈伤痛 2021-01-02 05:45

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

相关标签:
6条回答
  • 2021-01-02 06:10

    You just have to remove quotes in age variable because it's an integer, not a string:

      $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}]
    
    0 讨论(0)
  • 2021-01-02 06:17

    Changing your predicate to the following will also work:

    <th><a href="" ng-click="predicate = '1*age'; reverse=!reverse">Age</a></th>
    
    0 讨论(0)
  • 2021-01-02 06:21

    I know is a little late for this answer, but in the newest version of Angular, you can use a function as the orderBy's predicate (https://docs.angularjs.org/api/ng/filter/orderBy). In this case, add to the controller a function like next

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

    and change the predicate in the orderBy filter

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

    This will do the trick!

    0 讨论(0)
  • 2021-01-02 06:21

    Add JSON_NUMERIC_CHECK code while converting array into json. json_encode($anyarray,JSON_NUMERIC_CHECK);

    0 讨论(0)
  • 2021-01-02 06:33

    you have to convert age to type Number to make to orderBy to work as it should.

    Add to your controller to sort String age as float:

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

    It should work,

    See PLunker

    0 讨论(0)
  • 2021-01-02 06:35

    AngularJs have in built functionalities to sort the values.But before sorting float values we need to convert the values to float using the parseFloat() javavscript function .

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

    0 讨论(0)
提交回复
热议问题