thấy w3schools mẫu: https://www.w3schools.com/angular/angular_filters.asp https://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_orderby_click
sau đó thêm các "đảo ngược" cờ:
<!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>
Lưu ý nó ':' không phải là một dấu phẩy. (Đối với những người không quan sát khác) – ReactiveRaven
Nếu bạn muốn có một nút sắp xếp, bạn có thể thay thế đúng với sortDirection. Sau đó, trong phạm vi của bạn thiết lập $ scope.sortDirection = true. Nút bấm sẽ trông giống như ng-click = "sortDirection =! SortDirection" – mbokil
Đây là chỉ làm việc cho các trang có dữ liệu bảng đầy đủ trong một trang duy nhất, nhưng nó sẽ không làm việc cho các pagination .. – ANK