如何使用AngularJS对单击的列进行反向排序
我实现了一种对表列进行排序的简单方法,但我无法找到一种在单击和返回时反转排序的方法。有人能解决这个问题吗?下面是一把小提琴,告诉你我的意思如何使用AngularJS对单击的列进行反向排序,angularjs,sorting,angularjs-ng-click,Angularjs,Sorting,Angularjs Ng Click,我实现了一种对表列进行排序的简单方法,但我无法找到一种在单击和返回时反转排序的方法。有人能解决这个问题吗?下面是一把小提琴,告诉你我的意思 <div ng-app="app"> <div ng-controller="controller"> <p>{{orderProperty}}</p> <div class="col-md-10"> <table class="table table-ho
<div ng-app="app">
<div ng-controller="controller">
<p>{{orderProperty}}</p>
<div class="col-md-10">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Status<a ng-click="orderProperty = 'a'">^</a></th>
<th>Ref<a ng-click="orderProperty = 'b'">^</a></th>
<th>Service<a ng-click="orderProperty = 'c'">^</a></th>
<th>Domain<a ng-click="orderProperty = 'd'">^</a></th>
<th>Service Owner<a ng-click="orderProperty = 'e'">^</a></th>
<th>Stage<a ng-click="orderProperty = 'f'">^</a></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in projects | orderBy:orderProperty">
<td><b>{{x.a}}</b></td>
<td>{{x.b}}</td>
<td>{{x.c}}</td>
<td>{{x.d}}</td>
<td>{{x.e}}</td>
<td>{{x.f}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
{{orderProperty}}
状态您可以通过传递true
作为orderBy
过滤器的第三部分来反转排序:
<tr ng-repeat="x in projects | orderBy : orderProperty : true">
在html中
更改ng单击
调用函数
<tr>
<th><a ng-click="sortProperty('a')">Status</a></th>
<th><a ng-click="sortProperty('b')">Ref</a></th>
<th><a ng-click="sortProperty('c')">Service</a></th>
<th><a ng-click="sortProperty('d')">Domain</a></th>
<th><a ng-click="sortProperty('e')">Service Owner</a></th>
<th><a ng-click="sortProperty('f')">Stage</a></th>
</tr>
单击列标题将切换排序
您可以通过在列名前面加“-”来切换orderProperty。
将表格标题替换为以下代码:
<thead>
<tr>
<th>Status<a ng-click="setOrderProperty('a')">^</a></th>
<th>Ref<a ng-click="setOrderProperty('b')">^</a></th>
<th>Service<a ng-click="setOrderProperty('c')">^</a></th>
<th>Domain<a ng-click="setOrderProperty('d')">^</a></th>
<th>Service Owner<a ng-click="setOrderProperty('e')">^</a></th>
<th>Stage<a ng-click="setOrderProperty('f')">^</a></th>
</tr>
</thead>
啊,应该说得更清楚些。我想问的是,如何在单击时交替进行反向排序和正常排序。如何以十进制格式对数据进行排序。比如10.32,34.43.22.78
<thead>
<tr>
<th>Status<a ng-click="setOrderProperty('a')">^</a></th>
<th>Ref<a ng-click="setOrderProperty('b')">^</a></th>
<th>Service<a ng-click="setOrderProperty('c')">^</a></th>
<th>Domain<a ng-click="setOrderProperty('d')">^</a></th>
<th>Service Owner<a ng-click="setOrderProperty('e')">^</a></th>
<th>Stage<a ng-click="setOrderProperty('f')">^</a></th>
</tr>
</thead>
$scope.setOrderProperty = function(propertyName) {
if ($scope.orderProperty === propertyName) {
$scope.orderProperty = '-' + propertyName;
} else if ($scope.orderProperty === '-' + propertyName) {
$scope.orderProperty = propertyName;
} else {
$scope.orderProperty = propertyName;
}
}