You need to think a little back to do this. When you get the results for your query for the first page, remember the first element in the results:
firebaseRef.endAt().limitToLast(5).on('child_added', function(childSnapshot, prevChildKey) { self.addChild(childSnapshot);
While you cannot access child elements by index using Firebase, you can save the element key and use it to run the next query.
var firstKnownKey; firebaseRef.orderByKey().limitToLast(5).on('child_added', function(childSnapshot, prevChildKey) { if (!firstKnownKey) { firstKnownKey = childSnapshot.key; } self.addChild(childSnapshot);
You now have the variable firstKnownKey, which has the first key you have ever seen. To get the previous batch of children, you pass this value to endAt()
when you run the following query:
firebaseRef.orderByKey().endAt(firstKnownKey).limitToLast(5).on('child_added', function(childSnapshot, prevChildKey) { if (!firstKnownKey) { firstKnownKey = childSnapshot.key; } self.addChild(childSnapshot);
Answers to similar questions over the past few days:
source share