0

我有一个要根据搜索框进行过滤的分组元素。过滤器有效,但我的分页有问题,显示不包含过滤元素的页面的白页。当我在第 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);
});
4

1 回答 1

0

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

<div ng-repeat="team in teams | filter:searchInput | startFrom:(currentPage - 1)*pageSize  | limitTo:pageSize">
于 2013-09-13T01:48:54.863 回答