1

I'm using Bootstrap's Pagination directive to add pagination to a table.

What is so strange about this is that when I manually add to $scope.transactions (see commented out code in the controller), the pagination works perfectly fine. But when I try to populate $scope.transactions via an AJAX call, the table shows nothing. The AJAX call returns the following:

[{"date":"2017-01-06","amount":123,"action":"Deposit","id":1},{"date":"2017-01-06","amount":200,"action":"Deposit","id":2},{"date":"2017-01-06","amount":400,"action":"Deposit","id":3}]

View:

    <tr ng-repeat="transaction in f ">

    <td>{{transaction.id}}</td>
    <td>$ {{transaction.amount}}</td>
    <td>{{transaction.action}}</td>
    <td>{{transaction.date}}</td>
    </tr>
    </table>
    <pagination 
          ng-model="currentPage"
          total-items="transactions.length"
          max-size="maxSize"  
          boundary-links="true">
        </pagination>  

Controller:

 var mmyApp = angular.module('myApp',['ui.bootstrap']).controller('TasksController',  TasksController);
     function TasksController($scope, $http) {

     $scope.f = []
      ,$scope.currentPage = 1
      ,$scope.numPerPage = 3
      ,$scope.maxSize = 5;
     $scope.transactions = [];
    $scope.history = "";

    $http.get('transactions').then(function (response) {

         $scope.transactions = response.data;
     }, function (error) {
         throw error;
     });

     //if i use this commented out code instead of the AJAX call, the pagination works fine
     /*$scope.makeTransactions = function() {
            $scope.transactions = [];
            for (i=1;i<=1000;i++) {
              $scope.transactions.push({"id":i});
            }
          };
          $scope.makeTransactions();
          */
     $scope.$watch('currentPage + numPerPage', function() {
            var begin = (($scope.currentPage - 1) * $scope.numPerPage)
            , end = begin + $scope.numPerPage;

            $scope.f = $scope.transactions.slice(begin, end);
          });


 }

1 Answer 1

1

You forgot to populate $scope.fafter populating $scope.transactions in your ajax call. In your ng-repeat you're cycling through items that should be stored in $scope.f but it's empty.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much, this solved my problem! But there is still one more problem I was wondering if you could help me with. When this line is executed "total-items="transactions.length"", the current length of transactions is 0, so the additional pages are not showing up. I think this line is executed before the AJAX call is finished. Is there a way to dynamically set the total-items, perhaps after the AJAX call completes?
It should update. If it doesn't, try changing the binding to a function totalItems(): $scope.totalItems = function(){return $scope.transactions.length;}
I changed the binding to a function and I get the following error. Cannot read property 'length' of undefined at b.TasksController.$scope.totalItems
It seems like the scope isnt updating after the AJAX call, I tried calling $scope.$apply but I got an action already in progress error.
total-items="totalItems()"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.