0

I 'm a beginner in AngularJS. I have created a page that retrieves records from database along with total records using an Ajax call. After populating the grid with the records, I wanted to refresh the pager control based on values received from the Ajax call such as the total records and the current page number as 1. I'm seeing the pager but is not refreshed. Can you please verify and let me know what I'm missing here?

var angularPersonModule = angular.module("angularPersonModule", ['ui.bootstrap']);
angularPersonModule.controller("AngularPersonController", AngularPersonController);
angularPersonModule.controller("PaginationController", PaginationController);
angularPersonModule.factory("PaginationService", PaginationService);

PaginationController.$inject = ['$scope', '$timeout', '$rootScope', 'PaginationService'];

function PaginationController($scope, $timeout, $rootScope, PaginationService) {
  $scope.setPage = function(pageNo) {
    if ($scope.currentPage === pageNo) {
      return;
    }

    $scope.currentPage = pageNo;
    $rootScope.$broadcast("personPageChanged", $scope.currentPage);
    alert("call from setPage");
  };

  $scope.pageChanged = function() {
    var i = 0;
  };

  $scope.$on('personRefreshPagerControls', function(event, args) {
    //the solution:
    setTimeout(function() {
      //setTimeout is a deffered call (after 1000 miliseconds) 
      $timeout(function() {
        $scope.totalItems = PaginationService.getTotalRecords();
        $scope.currentPage = PaginationService.getPageNumber();
        $scope.itemsPerPage = PaginationService.getPageSize();
        $scope.numPages = PaginationService.getPageCount();
        alert("totalItems = " + $scope.totalItems);
        alert("currentPage = " + $scope.currentPage);
        alert("itemsPerPage = " + $scope.itemsPerPage);
        alert("pageCount = " + $scope.numPages);
      });

    }, 900);

  });
}

AngularPersonController.$inject = ['$scope', '$rootScope', 'AngularPersonService', 'PaginationService'];

function AngularPersonController($scope, $rootScope, AngularPersonService, PaginationService) {
  $scope.getAll = function() {
    $scope.persons = AngularPersonService.search();
    PaginationService.setTotalRecords(3);
    PaginationService.setPageNumber(1);
    PaginationService.setPageSize(2);
    PaginationService.setPageCount(2);
    $rootScope.$broadcast("personRefreshPagerControls", 3);
  }

  $scope.$on('personPageChanged', function(event, args) {
    alert("pageChanged Event fired");
    alert("Received args = " + args);
    $scope.getAll();
  });

  $scope.getAll();
}

function PaginationService() {
  var currentPage = 1;
  var pageCount = 2;
  var pageSize = 2;
  var totalRecords = 3;

  return {
    getPageNumber: function() {
      return currentPage;
    },
    setPageNumber: function(pageNumber) {
      currentPage = pageNumber;
    },
    getPageSize: function() {
      return pageSize;
    },
    setPageSize: function(newPageSize) {
      pageSize = newPageSize;
    },
    getTotalRecords: function() {
      return totalRecords;
    },
    setTotalRecords: function(newTotalRecords) {
      totalRecords = newTotalRecords;
    },
    getPageCount: function() {
      return pageCount;
    },
    setPageCount: function(newPageCount) {
      pageCount = newPageCount;
    }
  }
}

angularPersonModule.factory("AngularPersonService", function($http) {
  return {
    search: function() {
      return [{
        "Name": "Hemant"
      }, {
        "Name": "Ashok"
      }, {
        "Name": "Murugan"
      }];
    }
  }
});
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-resource.min.js"></script>
<script src="https://raw.github.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-0.14.3.js"></script>
<script src="https://raw.githubusercontent.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-0.14.3.js"></script>

<div ng-app="angularPersonModule">

  <div class="panel panel-default">
    <div class="panel-body" ng-controller="AngularPersonController">
      <table class="table table-striped table-hover">
        <thead>
          <tr>
            <th class="col-md-3">
              Name
            </th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="person in persons">
            <td>
              <span>{{person.Name}}</span>
            </td>
        </tbody>
      </table>
    </div>
  </div>
  <div ng-controller="PaginationController">
    <uib-pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()"></uib-pagination>
  </div>
</div>

Update

Now I have updated the snippet so that you can execute and see the result manually.

Hemant
  • 615
  • 1
  • 8
  • 21
  • Perhaps you could try to notify the view and controller that the new data has been fetched async using, $scope.$apply() – Fraction Nov 05 '15 at 15:42
  • Using $scope.$apply() is not a good practice, in some scenarios you can have an error: "$scope.$apply() isn't high enough in the call stack." instead you can use $timeout() [Best Practices to include code in the cycle digest in AngularJs](http://www.jomendez.com/2015/02/09/best-practice-to-include-code-within-the-cycle-digest/) – JoMendez Nov 05 '15 at 16:16
  • I have tried the $timeout but still don't see the pagination refreshed. I have edited my original listing for your reference. Can you please review and let me know if I have implemented $timeout correctly. – Hemant Nov 07 '15 at 10:00
  • I have updated the snippet and you can execute the snippet (in stackoverflow window itself) and see for yourself the pager control. It is only listing 1 page instead of 2 pages. – Hemant Nov 08 '15 at 08:06

0 Answers0