0

我正在尝试使用角度对我的列进行排序。我有角度教程的代码可以工作。

现在我很难让一排始终保持在顶部。例如,我将有一列称为 mainContact。此值只能设置一个人为真。我希望 mainContact 为 true 的人始终留在表格的第一行,而其他人则随着排序而变化。老实说,我对如何做到这一点的想法有点迷茫。

我把它放在一起来展示我到目前为止所拥有的:http: //jsfiddle.net/Beastwood/m44fy1j5/

<div ng-controller="ExampleController">
<pre>Sorting predicate = {{predicate}}; reverse = {{reverse}}</pre>
 <table class="friend">
<tr>
  <th><a href="" ng-click="predicate = 'name'; reverse=false">Name</a></th>
  <th><a href="" ng-click="predicate = 'phone'; reverse=!reverse">Phone Number</a></th>
  <th><a href="" ng-click="predicate = 'age'; reverse=!reverse">Age</a></th>
    <th>MAIN CONTACT</th>
</tr>
<tr ng-repeat="friend in friends | orderBy:predicate:reverse">
  <td>{{friend.name}}</td>
  <td>{{friend.phone}}</td>
  <td>{{friend.age}}</td>
     <td>{{friend.mainContact}}</td>
</tr>
</table>
</div>

JS文件

var myApp = angular.module('myApp', [])
.controller('ExampleController', ['$scope', function($scope) {
  $scope.friends =
      [{name:'John', phone:'555-1212', age:10, mainContact:true},
       {name:'Mary', phone:'555-9876', age:19, mainContact:false},
       {name:'Mike', phone:'555-4321', age:21, mainContact:false},
       {name:'Adam', phone:'555-5678', age:35, mainContact:false},
       {name:'Julie', phone:'555-8765', age:29, mainContact:false}];
   $scope.predicate = '-age';
   }]);
4

1 回答 1

2

您可以将一组属性名称传递给过滤器,并在属性名称前加上-to reverse...

<tr ng-repeat="friend in friends | orderBy:['-mainContact', (reverse ? '-' : '') + predicate]">
    ...
</tr>
  • -mainContact- 首先按mainContact属性以相反的顺序排序(因为 true = 1,false = 0)
  • (reverse ? '-' : '') + predicate- 然后按谓词排序,-如果反向为真,则附加一个

现场演示

于 2014-09-08T16:51:53.163 回答