2014-10-09 128 views
1

之前,我有一个ngRepeat ng-repeat="row in rows | orderBy:tableOrdering | filter:currentPage"角 - ngRepeat阶滤波器(分页+顺序)

  • 排序依据:tableOrdering将返回整数从1到1000的顺序对时尚。
  • 过滤器:当前页将从$ scope.rows返回结果的第一页(布尔)

因为filter:currentPage总是返回从scope.rows $,这不是有序的元素,只有结果上该(过滤)页面将被排序。

是否有一种方法可以运行OrderBy FIRST,这样它将排序所有行,然后在排序后通过currentPage进行过滤?

代码示例:http://plnkr.co/edit/CamEiYIxyW5TaUPgmHxD

+0

请告诉我这个问题? http://plnkr.co/edit/cRA0EuiEvUzwaLeO4ptr?p=preview过滤器不重新排列元素并在orderBy之后应用... – 2014-10-09 06:57:14

回答

1

使用prototype.some()功能,还给项目的原始阵列的指数(偏移)。这会导致问题,因为我们需要排序数组的索引。如果我们重构函数以使用与顺序函数相关的索引,那么它的效果很好。

HTML

<tr ng-repeat="item in items | orderBy:tableOrdering | filter:currentPage"> 

JS

var app = angular.module('plunker', []); 

app.controller('MainCtrl', function($scope) { 
    $scope.name = 'StackOverflow'; 
    $scope.items = [ 
     {id : 1, value : 2}, 
     {id : 2, value : 6}, 
     {id : 3, value : 5}, 
     {id : 4, value : 1}, 
     {id : 5, value : 0}, 
     {id : 6, value : 6}, 
     {id : 7, value : 8}, 
     {id : 8, value : 4}, 
     {id : 9, value : 6}, 
     {id : 10, value : 3} 
    ]; 

    $scope.pagination = { 
     limit: 3, // ITems per page 
     current: 0, // Current page (page - 1) 
     asc: true, // Asc Vs Desc 
     index: 0 // Count Variable for ordering/pagination 
    }; 

    $scope.setPage = function(page) { 
     $scope.pagination.current = page; 
    } 

    $scope.tableOrdering = function(item) { 
     $scope.pagination.index = 0; // Resets index for counting 
      if ($scope.pagination.asc) { 
       return parseFloat(item.value); 
      } 
      else { 
       return parseFloat(item.value) * (-1); 
      } 
    }; 

    $scope.currentPage = function(item) { 
     for (var k = 0; k < $scope.items.length; k++) { 
     if (item.id == $scope.items[k].id) { 
      $scope.pagination.index++; 
       if ( ($scope.pagination.limit * $scope.pagination.current)   <= $scope.pagination.index - 1 && 
        ($scope.pagination.limit * ($scope.pagination.current + 1) - 1) >= $scope.pagination.index - 1) { 

       // On page 
       return true; 
      } 
      else { 
       // Not on page 
       return false; 
      } 
     } 
     } 

     // Fallback 
     return false; 
    }; 

}); 

Plunker链接http://plnkr.co/edit/sFvM9Nc65DPCwLy5lofE?p=preview