This question already has answers here:
Angular orderBy Date
                            
                                (2个答案)
                            
                    
                5年前关闭。
        

    

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

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);
        }
    });
});

最佳答案

它可以工作,但显然不符合您的期望:代码中没有任何内容说该字段是Date,因此lastUpdateDate被当作普通的String处理,因此顺序是字母顺序的。

09-25 16:31