<div class="recent" ng-repeat="reader in
(filteredItems = (book.reader | orderBy: 'created_at' | limitTo: 1))">
</div>
所以這本書來自於休息api,它附有許多讀者。我想得到'最近'的讀者。AngularJs中的日期降序排列
created_at
字段具有將用戶識別爲最近的值。但上面的代碼給了我最老的讀者。所以訂單需要被倒置?有沒有辦法按降序排序?
<div class="recent" ng-repeat="reader in
(filteredItems = (book.reader | orderBy: 'created_at' | limitTo: 1))">
</div>
所以這本書來自於休息api,它附有許多讀者。我想得到'最近'的讀者。AngularJs中的日期降序排列
created_at
字段具有將用戶識別爲最近的值。但上面的代碼給了我最老的讀者。所以訂單需要被倒置?有沒有辦法按降序排序?
根據documentation,您可以使用reverse
參數。
filter:orderBy(array, expression[, reverse]);
您的過濾器更改爲:
orderBy: 'created_at':true
和代碼示例:
<div ng-app>
<div ng-controller="FooController">
<ul ng-repeat="item in items | orderBy:'num':true">
<li>{{item.num}} :: {{item.desc}}</li>
</ul>
</div>
</div>
和JavaScript:
function FooController($scope) {
$scope.items = [
{desc: 'a', num: 1},
{desc: 'b', num: 2},
{desc: 'c', num: 3},
];
}
會給你:
3 :: c
2 :: b
1 :: a
上的jsfiddle:http://jsfiddle.net/agjqN/
也許這可以USEF ul對於某人:
在我的情況下,我得到一個對象數組,每個對象都包含一個由Mongoose設置的日期。
我用:
ng-repeat="comment in post.comments | orderBy : sortComment : true"
而定義的函數:
$scope.sortComment = function(comment) {
var date = new Date(comment.created);
return date;
};
這爲我工作。
在我的情況下,orderBy由一個選擇框決定。我更喜歡路德維希的反應,因爲你可以在選擇的選項,例如設置排序方向:
$scope.options = [
{ label: 'Title', value: 'title' },
{ label: 'Newest', value: '-publish_date' },
{ label: 'Featured', value: '-featured' }
];
標記:
<select ng-model="orderProp" ng-options="opt as opt.label for opt in options"></select>
<ul>
<li ng-repeat="item in items | orderBy:orderProp.value"></li>
</ul>
這似乎不適用於自定義排序功能。有沒有辦法在使用自定義orderBy函數並選擇一個參數來從
降序排序按日期
這將有助於篩選記錄日期按降序排列。
$scope.logData = [
{ event: 'Payment', created_at: '04/05/17 6:47 PM PST' },
{ event: 'Payment', created_at: '04/06/17 12:47 AM PST' },
{ event: 'Payment', created_at: '04/05/17 1:50 PM PST' }
];
<div ng-repeat="logs in logData | orderBy: '-created_at'" >
{{logs.event}}
</div>
看到W3Schools的樣本: https://www.w3schools.com/angular/angular_filters.asp https://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_orderby_click
再加入 「反向」 的標誌:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<p>Click the table headers to change the sorting order:</p>
<div ng-app="myApp" ng-controller="namesCtrl">
<table border="1" width="100%">
<tr>
<th ng-click="orderByMe('name')">Name</th>
<th ng-click="orderByMe('country')">Country</th>
</tr>
<tr ng-repeat="x in names | orderBy:myOrderBy:reverse">
<td>{{x.name}}</td>
<td>{{x.country}}</td>
</tr>
</table>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.reverse=false;
$scope.orderByMe = function(x) {
if($scope.myOrderBy == x) {
$scope.reverse=!$scope.reverse;
}
$scope.myOrderBy = x;
}
});
</script>
</body>
</html>
注意它的':'不是一個逗號。 (對於其他不關心的人) – ReactiveRaven 2013-07-15 13:55:20
如果你想要一個排序按鈕,你可以用sortDirection替換true。然後在你的作用域中設置$ scope.sortDirection = true。點擊按鈕看起來像ng-click =「sortDirection =!sortDirection」 – mbokil 2016-02-23 17:38:04
這些僅適用於在單個頁面中具有完整表格數據的頁面,但它不適用於分頁.. – ANK 2016-05-07 11:39:24