AngularJs过滤分组元素和分页

时间:2013-09-13 01:07:15

标签: angularjs

我有一个需要基于搜索框过滤的分组元素。过滤器工作,但我有分页的问题,显示不包含元素过滤的页面的白页。当我在第1页并搜索第2页上的元素时,第一页仍为白色,我应该手动切换到第2页。

小提琴:http://jsfiddle.net/Tropicalista/qyb6N/9/

// create a deferred object to be resolved later
var teamsDeferred = $q.defer();

// return a promise. The promise says, "I promise that I'll give you your
// data as soon as I have it (which is when I am resolved)".
$scope.teams = teamsDeferred.promise;

// create a list of unique teams
var uniqueTeams = unique($scope.players, 'team');

// resolve the deferred object with the unique teams
// this will trigger an update on the view
teamsDeferred.resolve(uniqueTeams);

// function that takes an array of objects
// and returns an array of unique valued in the object
// array for a given key.
// this really belongs in a service, not the global window scope
function unique(data, key) {
    var result = [];
    for (var i = 0; i < data.length; i++) {
        var value = data[i][key];
        if (result.indexOf(value) == -1) {
            result.push(value);
        }
    }
    console.log(result)
    console.log(Math.ceil(result.length / 10))
    $scope.noOfPages = Math.ceil(result.length / 10);
    return result;
}

$scope.currentPage = 1;
$scope.pageSize = 5;
$scope.maxSize = 2;

[编辑]

我创建了一个观察搜索输入更改的函数:

$scope.$watch('searchInput', function () {
    $scope.currentPage = 1;
    $scope.teams = $filter('filter')($scope.teams, $scope.searchInput);
    $scope.noOfPages = Math.ceil($scope.teams.length / $scope.pageSize);
});

1 个答案:

答案 0 :(得分:0)

您需要调整过滤器的顺序。假设您要过滤团队,那么您需要在 分页过滤器之前应用搜索过滤器

<div ng-repeat="team in teams | filter:searchInput | startFrom:(currentPage - 1)*pageSize  | limitTo:pageSize">