0

我有一组用户,我想在第一次加载时按姓氏排序我的 ng-repeat。添加新用户后,按日期排序 ng-repeat,然后添加姓氏。本质上,我希望将最新用户推到 ng-repeat 的顶部。

<th ng-click="menuFilter('lastName', 1);">
  <div ng-class='{"menuSort":sortColumn==1}'>Name <span ng-show="share.orderByField == 'lastName'">
  </div>
</th>

 <tr ng-repeat="user in users | orderBy:orderByField:reverseSort"></tr>

在我的 JS...

_this.sortColumn = 1;
_this.orderByField = 'lastName';
_this.reverseSort = false;

 _this.menuFilter = function(section, column) {
      _this.orderByField = section;
      _this.reverseSort = !_this.reverseSort;
      _this.sortColumn = column;
};

//my attempt to reset the order by created at date
if( _this.isRefreshing ) { 
        _this.orderByField = ['createdAt', 'lastName'];
}

基本上这段代码没有做任何事情。我想我错过了 HTML 中的一步。

谢谢!

4

2 回答 2

0

我认为这是通过在纯 javascript 中对数组进行排序然后使用不带 orderBy 属性的 ng-repeat 来完成的。

HTML:

<div ng-repeat="user in users">{{user}}</div>

<input type="text" ng-model="name"></input>
<button ng-click="addName()">Add name</button>

JS:

$scope.users = ["Rusty", "Shackleford", "Dale", "Gribble"];
$scope.users.sort();

$scope.addName = function() {
   $scope.users.unshift($scope.name);
}

JSFiddle:http: //jsfiddle.net/asWF9/2/

这个答案可能有助于对您的数组进行排序:https ://stackoverflow.com/a/6712080/3675149

于 2014-06-17T07:47:49.543 回答
0

尝试使用“unshift”而不是“push”将项目添加到数组中。js 中的 unshift 使我们能够将一个项目插入到数组的顶部。

于 2014-06-17T10:49:56.840 回答