Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active February 24, 2022 06:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save katowulf/54fa9a1e713b61672f53 to your computer and use it in GitHub Desktop.
Save katowulf/54fa9a1e713b61672f53 to your computer and use it in GitHub Desktop.
Simple paginate example in AngularFire with Firebase.util (http://firebase.github.io/firebase-util/)
var app = angular.module('app', ['firebase']);
app.controller('ctrl', function($scope, $pageArray) {
$scope.pageItems = $pageArray(ref, 'number');
});
app.factory('$pageArray', function($firebaseArray) {
return function(ref, field) {
// create a Paginate reference
var pageRef = new Firebase.util.Paginate(ref, field, {maxCacheSize: 250});
// generate a synchronized array using the special page ref
var list = $firebaseArray(pageRef);
// store the "page" scope on the synchronized array for easy access
list.page = pageRef.page;
// when the page count loads, update local scope vars
pageRef.page.onPageCount(function(currentPageCount, couldHaveMore) {
list.pageCount = currentPageCount;
list.couldHaveMore = couldHaveMore;
});
// when the current page is changed, update local scope vars
pageRef.page.onPageChange(function(currentPageNumber) {
list.currentPageNumber = currentPageNumber;
});
// load the first page
pageRef.page.next();
return list;
}
});
<body ng-app="app" ng-controller="ctrl">
<h4>Showing: Page {{ pageItems.currentPageNumber }} of {{ pageItems.pageCount }}<span ng-show="pageItems.couldHaveMore"> or more</span></h4>
<ul>
<li ng-repeat="item in scrollItems">{{item.$id}}: {{item.string}}</li>
</ul>
<p>
<button class="btn btn-primary"
ng-click="pageItems.page.setPage(1)"
ng-disabled="pageItems.currentPageNumber < 2">
<i class="glyphicon glyphicon-fast-backward"></i>
</button>
<button class="btn btn-primary"
ng-click="pageItems.page.prev()"
ng-disabled="!pageItems.page.hasPrev()">
<i class="glyphicon glyphicon-backward"></i>
</button>
<button class="btn btn-primary"
ng-click="pageItems.page.next()"
ng-disabled="!pageItems.page.hasNext()">
<i class="glyphicon glyphicon-forward"></i>
</button>
<button class="btn btn-primary"
ng-click="pageItems.page.setPage(pageItems.pageCount)"
ng-disabled="!pageItems.page.hasNext() || pageItems.currentPageNumber >= pageItems.pageCount">
<i class="glyphicon glyphicon-fast-forward"></i>
</button>
</p>
</body>
@PrashantYadav03
Copy link

How to include pageArray service, I get error when i try to add the dependencies in my controller

@lokeshjain2008
Copy link

@prashant you can follow link

@prgwar
Copy link

prgwar commented Feb 2, 2017

Hi what is the field option in
var pageRef = new Firebase.util.Paginate(ref, field, {maxCacheSize: 250});

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