日期列未在ng-table中排序

时间:2014-10-07 14:42:17

标签: javascript angularjs sorting ngtable

我已经使用ng-table创建了一个应用程序,该应用程序工作正常,排序工作正常,除了日期列,任何人都可以告诉我一些解决方案。我的代码如下所示

Working Demo

HTML

<table ng-table="tableParams" class="table">
    <tr ng-repeat="user in myValues">
        <td data-title="'Name'" sortable="'name'">
            {{user.name}}
        </td>
        <td data-title="'Age'" sortable="'age'">
            {{user.age}}
        </td>
        <td data-title="'Last Update Date'" sortable="'lastUpdateDate'">
            {{user.lastUpdateDate}}
        </td>
    </tr>
</table>

脚本

var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, $filter, ngTableParams) {
    $scope.myValues = [{name: "Moroni", age: 50, lastUpdateDate:"03/14/2014 06:21"},
                {name: "Tiancum", age: 43, lastUpdateDate:"07/24/2014 06:41"},
                {name: "Jacob", age: 27, lastUpdateDate:"09/10/2001 04:46"},
                {name: "Nephi", age: 29, lastUpdateDate:"03/30/2013 03:31"},
                {name: "Enos", age: 34, lastUpdateDate:"02/20/2004 09:11"},
                {name: "Tiancum", age: 43, lastUpdateDate:"07/12/2012 05:24"},
                {name: "Jacob", age: 27, lastUpdateDate:"01/24/2013 01:11"},
                {name: "Nephi", age: 29, lastUpdateDate:"05/02/2014 02:21"},
                {name: "Enos", age: 34, lastUpdateDate:"01/21/2001 03:31"},
                {name: "Tiancum", age: 43, lastUpdateDate:"02/14/2002 04:21"},
                {name: "Jacob", age: 27, lastUpdateDate:"03/22/2022 05:01"},
                {name: "Nephi", age: 29, lastUpdateDate:"04/14/2020 06:11"},
                {name: "Enos", age: 34, lastUpdateDate:"05/03/2002 07:03"}, 
                {name: "Tiancum", age: 43, lastUpdateDate:"06/09/2000 08:01"},
                {name: "Jacob", age: 27, lastUpdateDate:"07/10/2001 09:02"},
                {name: "Nephi", age: 29, lastUpdateDate:"08/27/2012 10:10"},
                {name: "Enos", age: 34, lastUpdateDate:"09/12/2014 06:12"}];

    $scope.tableParams = new ngTableParams({
        sorting: {
            lastUpdateDate: 'desc'     
        }
    }, {
        getData: function($defer, params) {
           $scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
          $defer.resolve($scope.myValues);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

它可以工作,但显然不符合您的预期:您的代码中没有任何内容表示该字段是日期,因此lastUpdateDate作为公共字符串处理,因此顺序是按字母顺序排列的。