简体   繁体   中英

ng-Grid Pagination not working - AngularJS

HTML:

<div class="grid-style" data-ng-grid="groupGrid">
     </div>

Javascript:

appRoot.controller('GroupController', function ($scope, $location, $resource, $http) {

var groupResource = $resource('/api/group', {}, { update: { method: 'PUT' }, add: {      method: 'POST'} });
$scope.groupList = [];    

groupResource.query(function (data) {
    $scope.groupList.length = 0;
    angular.forEach(data, function (groupData) {
        $scope.groupList.push(groupData);
    });
});

$scope.totalServerItems = 0;

$scope.pagingOptions = {
    pageSizes: [4, 8, 12, 16],
    pageSize: 250,
    currentPage: 1
};
$scope.setPagingData = function (data, page, pageSize) {
    var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
    $scope.groupList = pagedData;
    $scope.totalServerItems = data.length;
    $scope.currentPage = page;
    if (!$scope.$$phase) {
        $scope.$apply();
    }
};

$scope.getPagedDataAsync = function (pageSize, page, searchText) {
    setTimeout(function () {
        var data;
        if (searchText) {
            var ft = searchText.toLowerCase();
            $http.get('/api/group').success(function (largeLoad) {
                data = largeLoad.filter(function (item) {
                    return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                });
                $scope.setPagingData(data, page, pageSize);
            });
        } else {
            $http.get('/api/group').success(function (largeLoad) {
                $scope.setPagingData(largeLoad, page, pageSize);
            });
        }
    }, 100);
};

$scope.getPagedDataAsync($scope.pagingOptions.pageSize,     $scope.pagingOptions.currentPage);

$scope.$watch('pagingOptions', function (newVal, oldVal) {
    if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
    }
}, true);

$scope.selectedGroups = [];

$scope.groupGrid = {
    data: 'groupList',
    multiSelect: false,
    selectedItems: $scope.selectedGroups,
    enableColumnResize: false,                        
    enablePaging: true,
    showFooter: true,
    totalServerItems: 'totalServerItems',
    pagingOptions: $scope.pagingOptions,
    columnDefs: [
        { field: 'groupName', displayName: 'Group Name', width: '25%' }
    ],
};
});

My problem - Pagination is not working, as in - The current page number is not displayed. - When I change the page size (like say 4), the number of items in the grid doesn't change (remains 16 - total no. of items I actually have). - The number of items change in the grid only after clicking the next page button twice.

Is your 'pagingOptions.currentPage' input max-invalid?

https://github.com/angular-ui/ng-grid/issues/598

You just had to update ng-grid to the latest stuff

I've searched for quite a while to do some pagination in ng-grid, but I never got what I wanted. And then I bumped into ng-table .

It seems to have what I want, so I suggest that as an alternative for ng-grid.

To get the current page in ng-grid, pass exact int value to the cuurent page variable, for that I changed in my code as follows.

$scope.pagingOptions = {
    pageSizes: [4, 8, 12, 16],
    pageSize: 250,
    currentPage: parseInt(pageId,10);
};

pageId = 1 or 2 or 3 etc.. as your wish.

Add below code in your js file.

var pageop=false;
        $scope.$watch('pagingOptions.pageSize', function (newVal, oldVal) {
            if(pageop){
  $scope.getPagedDataAsync($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage);                 

            }else{
                 pageop=true;
             }

        }, true); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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