简体   繁体   English

使用$ filter和谓词函数数组的角多列排序

[英]Angular multi-column sort using $filter and predicate function array

I need to order an array using the angular $filter('orderBy') function, but need to specify three predicate callbacks, and the sort order needs to be ASC, DESC, ASC. 我需要使用angular $filter('orderBy')函数对数组进行排序,但是需要指定三个谓词回调,并且排序顺序必须为ASC,DESC,ASC。

I know that there are many ways to solve the problem of multi-column sort, but my question is specifically about using Angular's orderBy filter to do it. 我知道有很多方法可以解决多列排序的问题,但是我的问题特别是关于使用Angular的orderBy过滤器来做到这一点。

The examples that I provide are not the actual data, but rather just a simplified model so that it is easier to explain. 我提供的示例不是实际数据,而是一个简化的模型,以便于解释。

I'm aware that you can use simple property names to handle it like this: 我知道您可以使用简单的属性名称来处理它,如下所示:

$scope.arr = $filter('orderBy')($scope.arr, ['+year', '-month', '+payment']);

However, my orderBy code looks like this: 但是,我的orderBy代码如下所示:

$scope.arr = $filter('orderBy')($scope.arr, [
      function(row) {
          //complicated logic here...for demo just return the raw property  
          return row.year;
      },
      function(row) {
          //complicated logic here...for demo just return the raw property
          return row.month;
      },
      function(row) {
          //complicated logic here...for demo just return the raw property
          return row.payment;
      }
    ], true);
  }

With my array of functions method, I cannot seem to find a way to provide ASC, DESC, ASC sorting order. 使用我的函数数组方法,似乎无法找到一种提供ASC,DESC,ASC排序顺序的方法。 I can use the third argument to flip the whole array ( $filter('orderBy')($scope.arr,[...], true) ), but that's not what I want. 我可以使用第三个参数来翻转整个数组( $filter('orderBy')($scope.arr,[...], true) ),但这不是我想要的。

I've tried forming the array like this, but that didn't work either: 我试过像这样形成数组,但是那也不起作用:

['+', function(row){return row.year;}, '-', function(row){return row.month}, '+', function(row){return row.payment;}]

I'm out of ideas. 我没主意了。 Is there any way to use the predicate function callbacks and still get the per-property specified direction sorting? 有什么方法可以使用谓词函数回调并仍然获取每个属性指定的方向排序?

 angular.module('app', []).controller('MyController', function($scope, $filter) { var id = 0; var arr = []; //generate some dummy data for (var year = 2000; year < 2010; year++) { for (var month = 3; month > 0; month--) { for (var payment = 100; payment < 300; payment = payment + 50) { var row = { id: id++, year: year, month: '0' + month, payment: payment }; arr.push(row); } } } $scope.arr = arr; $scope.order = function() { $scope.arr = $filter('orderBy')($scope.arr, [ function(row) { return row.year; }, function(row) { return row.month; }, function(row) { return row.payment; } ], true); } //sort the array right away $scope.order(); }); 
 th { padding: 10px 20px; border: 1px solid black; background-color: lightgrey; text-align: center; } td { padding: 10px 20px; border: 1px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> <div ng-app="app"> <div ng-controller="MyController"> <table> <tr> <th>ID</th> <th>Year</th> <th>Month</th> <th>Payment</th> </tr> <tr ng-repeat="row in arr"> <td>{{row.id}}</td> <td>{{row.year}}</td> <td>{{row.month}}</td> <td>{{row.payment}}</td> </tr> </table> </div> </div> 

Can you try this? 你可以试试这个吗?

function(row) {
      //complicated logic here...for demo just return the raw property  
      return row.year;
  },
  function(row) {
      //complicated logic here...for demo just return the raw property
      return -1*row.month;
  },
  function(row) {
      //complicated logic here...for demo just return the raw property
      return row.payment;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM