Angular js - 基于字母ASCII值在表格中排序

时间:2014-03-12 10:45:38

标签: javascript angularjs sorting

我已经使用angular完成了一个基本的表排序应用程序,排序正在进行但问题是我没有得到以ASCII值排序的值

任何人都可以告诉我该怎么做

[DEMO]

HTML

    <div ng-app ng-controller="ArrayController">
        <table>
            <th ng-repeat="header in headers">
                <a ng-click="sort_by(header)">{{ headers[$index] }}</a>
            </th>
            <tr ng-repeat="arr in records |orderBy:sortingOrder:reverse">
                <td ng-repeat="val in arr" ng-bind-html-unsafe="arr[headers[$index]]"></td>
            </tr>
        </table>
    </div>

脚本

    function ArrayController($scope) {
    $scope.headers = ['col1', 'col2'];
    $scope.records = [{col1: 'a1', col2: 'd1'}, {col1: 'c2', col2: 'A2'}, {col1: 'b3', col2: 'c3'}, {col1: 'd4', col2: 'a1'}];

    $scope.reverse = true;
     $scope.sort_by = function(newSortingOrder)
        {
            if ($scope.sortingOrder === newSortingOrder)
            {
              $scope.reverse = !$scope.reverse;
            }
            $scope.sortingOrder = newSortingOrder;

          };
}

1 个答案:

答案 0 :(得分:1)

您需要编写自定义orderBy函数,请查看this post以获取更多详细信息。