在分页之前按数组排序

时间:2015-08-06 12:43:26

标签: javascript angularjs pagination

我有以下HTML:

<div class="creations accordion" ng-repeat="creation in filteredCreations | orderBy : field : reverse">
   [[creation.id]]
   [[creation.name]]
</div>

角度控制器:

var exampleApp = angular.module('exampleApp', ['ui.bootstrap']).config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[').endSymbol(']]');
});

exampleApp.controller('CreationController', ['$scope', '$filter', function($scope, $filter) {
    $scope.currentPage = 1;
    $scope.itemsPerPage = 2;

    creations = $.map(creations, function(value, index) {
        return [value];
    });
    $scope.creations = creations;
    $scope.filteredCreations = [];

    $scope.order = function(field) {
      $scope.reverse = ($scope.field === field) ? !$scope.reverse : false;
      $scope.field = field;
    };
    $scope.field = 'id';
    $scope.reverse = false;

    $scope.figureOutCreationsToDisplay = function() {
        var begin = (($scope.currentPage - 1) * $scope.itemsPerPage)
        , end = begin + $scope.itemsPerPage;

        $scope.filteredCreations = $scope.creations.slice(begin, end);
    };

    $scope.figureOutCreationsToDisplay();

    $scope.pageChanged = function() {
        $scope.figureOutCreationsToDisplay();
    };
}]);

因此,每次页面更改时,我都会对结果进行分页,然后显示分页结果。这意味着orderBy过滤器仅对分页集上的数据进行排序,而不是所有结果。

我想对所有数据进行排序,然后对其进行分页。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

在渲染列表之前,您必须在控制器中执行orderBy。然后,如果您有任何更改orderby顺序的触发器,您只需要在控制器中调用sort函数。这是一个如何在控制器中利用角度orderBy的链接(所以你不必自己编写)。

$scope.filteredCreations = $filter('orderBy')(itemsArray, "field")

http://sravi-kiran.blogspot.com/2013/12/InvokingAngularJsFiltersFromController.html