按降序进行角度排序

Angular sorting by descending

本文关键字:排序 降序      更新时间:2023-09-26

我认为这是在角度中使用 orderby 过滤器,我们可以通过在您想要排序的过滤器之前添加一个"-"来简单地反转。因此,在下面的代码中,为什么这不起作用?我尝试在单引号内外使用"-"。

法典:

<tr class="alertText" ng-repeat="swipe in ppt.Swipes | orderBy:'-swipe.date' ">
    <td>{{swipe.date|date : 'MM/dd/yyyy'}}</td>
    <td>{{swipe.descr}}</td>
    <td>{{swipe.amount | currency}}</td>
    <td>{{swipe.dueDate|date : 'MM/dd/yyyy'}}</td>
    <td>{{swipe.status}}</td>
    <td class="clearIcon"><span ng-click="editSwipe()"><img src="ppt/assets/toolIcons/clearswipe-small.svg"></span></td>
</tr>

orderBy:'-date'就足够排序了

你应该在filter的ng-repeat中将属性作为'-date',

法典:

<div ng-repeat="card in myCards | orderBy:'-date'">
        <businesscard>{{card.firstName}} {{card.date | date}}</businesscard>
    </div>

这是工作jsfiddle

function Main($scope) {
    $scope.items = [
      {"name":"ali",date: new Date('12/23/2013')}
      ,{"name":"Reza",date: new Date('12/23/2015')},
      {"name":"Amir",date: new Date('12/23/2011')}
    ];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <div ng-controller="Main">
      <div ng-repeat="item in items | orderBy:'-date'">{{item.name}}: {{item.date | date}}</div>
    </div>
</div>

并且不要忘记,您可以使用 orderBy filter 的第二个参数来设置排序顺序:

<tr class="alertText" ng-repeat="swipe in ppt.Swipes | orderBy:'date':1">