我正在开发一个使用Angular 1.4.9的应用程序,而且我遇到了按日期排序表列的一些问题。
这是我的代码:
HTML
[...]
<table class="table table-hover">
<thead>
<tr>
<th class="header-table-names">
<a href="" data-ng-click="ctrl.order('creationDate')">
Creation date
<span data-ng-show="order.predicate === 'creationDate' && !order.reverse" class="fa fa-caret-down"></span>
<span data-ng-show="order.predicate === 'creationDate' && order.reverse" class="fa fa-caret-up"></span>
</a>
</th>
<th class="header-table-names">
<a href="" data-ng-click="ctrl.order('sendDate')">
Send date
<span data-ng-show="order.predicate === 'sendDate' && !order.reverse" class="fa fa-caret-down"></span>
<span data-ng-show="order.predicate === 'sendDate' && order.reverse" class="fa fa-caret-up"></span>
</a>
</th>
<th class="header-table-names">
<a href="" data-ng-click="ctrl.order('type')">
Type
<span data-ng-show="order.predicate === 'type' && !order.reverse" class="fa fa-caret-down"></span>
<span data-ng-show="order.predicate === 'type' && order.reverse" class="fa fa-caret-up"></span>
</a>
</th>
<th class="header-table-names">
<a href="" data-ng-click="ctrl.order('subject')">
Type
<span data-ng-show="order.predicate === 'subject' && !order.reverse" class="fa fa-caret-down"></span>
<span data-ng-show="order.predicate === 'subject' && order.reverse" class="fa fa-caret-up"></span>
</a>
</th>
</tr>
</thead>
<tbody>
<tr class="document-list-element" data-ng-repeat="doc in documents">
<td>{{doc.date | date:'dd/MM/yyyy'}}</td>
<td>{{doc.dateAdded | date:'dd/MM/yyyy - HH:mm'}}</td>
<td>{{doc.type}}</td>
<td>{{doc.subject}}</td>
</tr>
</tbody>
</table>
[...]
JAVASCRIPT
[...]
function order(predicate) {
order.reverse = (order.predicate === predicate) ? !order.reverse : false;
order.predicate = predicate;
loadDocuments(true);
}
[...]
function loadDocuments(docs, order) {
var temp = docs;
if (order === null) {
return temp;
}
temp = $filter('orderBy')(temp, order.predicate, order.reverse);
return temp;
}
[...]
以下是doc对象的属性Date
的示例:
Date 2016-01-04T19:32:08.452Z
Date 2016-03-17T18:11:21.702Z
按类型和主题的顺序工作没有问题。按日期的顺序不是。它似乎正确地订购了文件,但它实际上没有。