Angular orderBy过滤器不自然排序

时间:2014-08-25 18:42:33

标签: angularjs

我有以下代码:

<html ng-app="myApp">
    <head>
        <title>Angular Demo</title>
        <script src="angular/angular.min.js"></script>
        <script src="js/controllers.js"></script>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
    </head>
    <body>
        <div ng-controller="myController">
            <div ng-init="oras='Valcea'"></div>
            <input type="text" ng-model="search"><br/><br/>
            <select name="" id="" ng-model="eleviorder">
                <option value="name">Nume</option>
                <option value="prenume">Prenume</option>
                <option value="age">Varsta</option>
                <option value="highscool">Liceu</option>
                <option value="luckynumber">Numar norocos</option>
            </select>
            Ordonare Nume:<label>
                <input type="radio" name="direction" ng-model="direction" checked/>ascending</label>
            <label> 
                <input type="radio" name="direction" ng-model="direction" value="reverse"/>descending
            </label>
            <ul>
                <li ng-repeat="item in elevi | filter :search | orderBy :eleviorder :direction :myfunction">
                    <div>{{item.name}} {{item.prenume}}, Varsta:{{item.age}}, Liceu: {{item.highscool}}, Numar norocos: {{item.luckynumber}} Oras:{{oras}}</div>
                </li>
            </ul>
        </div>
    </body>
</html>

使用此控制器

var myApp = angular.module('myApp', []);

myApp.controller('myController',['$scope','$http',function ($scope,$http){
    $http.get('js/data.json').success(function(data){
        $scope.elevi = data;
        $scope.eleviorder = 'name';
    });
}]);

谁从data.json

获取此数据
[
    {"name":"Udrea",
    "prenume":"Alexandru",
    "age":"27",
    "highscool":"Henri Coanda",
    "luckynumber":"7"
    },
    {"name":"Rizea",
    "prenume":"Paul",
    "age":"23",
    "highscool":"Mircea Cel Batran",
    "luckynumber":"11"
    },
    {"name":"Popescu",
    "prenume":"Ion",
    "age":"19",
    "highscool":"Pedagogic",
    "luckynumber":"17"
    },
    {"name":"Popescu",
    "prenume":"Maria",
    "age":"18",
    "highscool":"Sanitar",
    "luckynumber":"8"
    },
    {"name":"Muscalu",
    "prenume":"Leonard",
    "age":"22",
    "highscool":"Forestier",
    "luckynumber":"47"
    },
    {"name":"Ionescu",
    "prenume":"Ema",
    "age":"29",
    "highscool":"Alexandru Lahovari",
    "luckynumber":"26"
    }
]

我的问题是,我不能按照自然顺序从运气数量单位订购运气数据,如11,17,26,47,7,8而不是7,8,11,17,26,47 < / p>

1 个答案:

答案 0 :(得分:0)

那是因为angularjs认为这个字段是一个字符串属性。因此它按字母顺序排序。如果从整数属性的值中删除引号,它应该正常工作。

[
    {"name":"Udrea",
    "prenume":"Alexandru",
    "age":27,
    "highscool":"Henri Coanda",
    "luckynumber":7
    },
    {"name":"Rizea",
    "prenume":"Paul",
    "age":23,
    "highscool":"Mircea Cel Batran",
    "luckynumber":11
    },...
]