Created
November 15, 2016 13:06
-
-
Save maynagashev/624ef4211d267e00f22332596df490d6 to your computer and use it in GitHub Desktop.
Angular Simplest Pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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>