2015-09-13 79 views
0

我有一些数据,我正在使用角度和数据表显示。现在我希望这些数据按日期排序。我的代码看起来像这样。排序和格式化日期角

<table dataTable="ng" 
     class="table table-striped table-bordered hover" 
     dt-options="dtOptions"> 
    <thead> 
    <tr > 
     <th>Date/Time</th> 
     <th>Created By</th> 
     ... 
    </tr> 
    </thead> 
    <tbody> 
    <tr ng-repeat="x in items | orderBy : 'timestamp' : true"> 
     <td>{{x.timestamp | date: 'medium'}}</td> 
     <td>{{x.user.firstname}} {{x.user.lastname}}</td> 
    ... 
    </tr> 
    </tbody> 
</table> 

但是,似乎我只能格式化日期或排序它,但不是两个在一起。如果我删除它的排序格式,否则它不会。

任何人都知道我在做什么错了?

回答

0

它没有问题(运行代码片段...)。

这是你想要做的吗?

请注意,我删除了表格指令并将其替换为ng-controller,这可能会导致您的问题?

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

 
angular 
 
    .module('myApp') 
 
    .controller('myTable', MyTable) 
 

 
function MyTable($scope){ 
 
    $scope.items = [ 
 
     { 
 
     timestamp : 1442188000000, 
 
     user: { 
 
      firstname : 'George' 
 
     } 
 
     }, 
 
     
 
     { 
 
     timestamp : 1442189022149, 
 
     user: { 
 
      firstname : 'John' 
 
     } 
 
     }, 
 
     { 
 
     timestamp : 1442199022149, 
 
     user: { 
 
      firstname : 'David' 
 
     } 
 
     } 
 
    ] 
 
}
\t <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> 
 

 

 
<table ng-app="myApp" ng-controller="myTable"> 
 
    <thead> 
 
    <tr > 
 
     <th>Date/Time</th> 
 
     <th>Created By</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr ng-repeat="x in items | orderBy : 'timestamp' : true"> 
 
     <td>{{x.timestamp | date: 'medium'}}</td> 
 
     <td>{{x.user.firstname}} {{x.user.lastname}}</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+0

看来这个问题是我的时间戳的格式。它是一个日期类型。它从c#DatetimeOffset中序列化。也许这是问题,因为它不会正确排序。 –

+0

时间戳的格式如下:timestamp:“2015-09-11T00:00:00 + 10:00” –