自定义排序函数,不区分大小写

时间:2016-01-08 14:38:20

标签: javascript angularjs

我有一个ng-repeat,其中我是orderBy-function。我注意到它在案例敏感性方面没有像我希望的那样工作

在我的例子中,你会看到" BBBBB"来自" aaaaa"之前,这在我的情况下是错误的。 " AAAAA"应在" BBBBB"。

之前订购

我不知道怎么做。任何人都可以帮助我吗?

http://plnkr.co/edit/ICNuVCkotSaIsfvdQGTt?p=preview

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example105-production</title>
<link href="style.css" rel="stylesheet" type="text/css">


<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<script src="script.js"></script>



</head>
<body ng-app="orderByExample">
<div ng-controller="ExampleController">
<table class="friend">

<tr>
  <th>
     <button ng-click="order('group')">Name</button>
     <span class="sortorder" ng-show="predicate === 'group'" ng-class="{reverse:reverse}"></span>
 </th>
<tr ng-repeat="friend in friends | orderBy:predicate:reverse">
  <td>{{friend.group}}</td>
</tr>
</table>
</div>
</body>
</html>

JS:

(function (angular) {
    'use strict';
    angular.module('orderByExample', [])
        .controller('ExampleController', [
            '$scope',
            function ($scope) {
                $scope.friends = [
                   { group: ['aaaaa'] },
                   { group: ['BBBBB'] },
                   { group: ['AAAA'] },
                ];
                $scope.predicate = 'group';
                $scope.reverse = false;
                $scope.order = function (predicate) {
                    $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
                    $scope.predicate = 'group';
                };
            }
        ]);
})(window.angular);

3 个答案:

答案 0 :(得分:1)

将谓词更改为函数:

$scope.predicate = function(d) { return d.group[0].toLowerCase(); };

答案 1 :(得分:1)

问题实际上并非如此。这是因为您的数据不是由字符串组成的。将数据结构更改为:

$scope.friends =
    [{group:'aaaaa'},
    {group:'BBBBB'},
    {group:'AAAA'},
];

答案 2 :(得分:0)

将谓词更改为

   $scope.predicate = function(item) {
     return item.group.join('').toUpperCase();
  };