Skip to content

Instantly share code, notes, and snippets.

@maynagashev
Created November 15, 2016 13:06
Show Gist options
  • Save maynagashev/624ef4211d267e00f22332596df490d6 to your computer and use it in GitHub Desktop.
Save maynagashev/624ef4211d267e00f22332596df490d6 to your computer and use it in GitHub Desktop.
Angular Simplest Pagination
var perPage = 10;
var items = [];
// init call
$scope.pagination = pagination(items, perPage, 1);
// subsequent calls (usually a method inside controller for ng-click)
this.showPage = function (page) {
$scope.pagination = pagination($scope.pagination.items, perPage, page);
};
// reference
function pagination(items, perPage, curPage) {
var pagesCount = Math.floor((items.length%perPage===0) ? items.length/perPage : items.length/perPage+1);
curPage = (Number.isInteger(curPage) && curPage>=1 && curPage<=pagesCount) ? curPage : 1;
var r = {
curPage : curPage,
start : curPage*perPage-perPage,
finish : curPage*perPage-1,
length : items.length,
perPage : perPage,
pagesCount : pagesCount,
items : items,
list : [],
pages : []
};
r.list = r.items.slice(r.start,r.finish+1);
for(var i=0; i<pagesCount; i++) { r.pages[i] = i+1; }
console.log(r);
return r;
}
@maynagashev
Copy link
Author

maynagashev commented Nov 15, 2016

Super simple example of template:
<button ng-repeat="p in pagination.pages" ng-click="c.showPage(p)" class="btn btn-warning" style="margin-right: 10px;">{{p}}</button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment