0

Is there any easy option to update pagination and filteredTodos when delete items from todos Demo: http://codepen.io/anon/pen/gpveqL

var todos = angular.module('todos', ['ui.bootstrap']);
todos.controller('TodoController', function($scope) {
  $scope.filteredTodos = [];
  $scope.currentPage = 1;
  $scope.numPerPage = 10;
  $scope.maxSize = 5;
  $scope.makeTodos = function() {
  $scope.todos = [];
    for (i = 1; i <= 66; i++) {
      $scope.todos.push({id: i, text: 'todo ' + i, done: false});
    }
  };
  $scope.makeTodos();
  $scope.$watch('currentPage + numPerPage', function() {
    var begin = (($scope.currentPage - 1) * $scope.numPerPage),
      end = begin + $scope.numPerPage;
    $scope.filteredTodos = $scope.todos.slice(begin, end);
  });
  $scope.deleteMe = function (id) {
    $scope.todos.splice(0, 1);
  };
});
<pagination 
 ng-model="currentPage"
 total-items="todos.length" 
 max-size="maxSize" 
 boundary-links="true" 
 previous-text="&lsaquo;" 
 next-text="&rsaquo;" 
 first-text="&laquo;" 
 last-text="&raquo;"
></pagination>
Mo.
  • 26,306
  • 36
  • 159
  • 225
  • Use a filter instead of a different array. http://stackoverflow.com/questions/31172426/connecting-angular-bootstrap-ui-paginate-to-table/31182565#31182565 – jme11 Jul 02 '15 at 20:27
  • 1
    Updated [CodePen](http://codepen.io/jme11/pen/mJXjyZ?editors=101). The reason this works is that the filter is non-destructive. Filters return a new array that is used by the repeat (or piped to the next filter). ngRepeat automatically watches the scoped array that it is bound to so you don't need the extra watch. More details about creating a custom filter is in the other question I linked (which I literally answered earlier today). – jme11 Jul 02 '15 at 21:53

0 Answers0