A
A
Andrew2019-11-01 09:18:53
firebase
Andrew, 2019-11-01 09:18:53

Pagination in firebase?

How to make pagination in firebase? Firebase does not have analogues of count and skip, which does not allow you to simply enter the page number and get the desired segment of records. There is a startAfter method that requires us to specify a specific entry there, after which we need to look for other entries.
The current mechanisms are suitable for infinite scrolling, when we know for sure this very previous entry. But what to do when you need to get records based on url? (e.g. "url.com/blog/3")
Load all posts and cut from array? Rave. And how to find out how many pages we have in total? Load all the records every time and count? Again nonsense.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2019-11-01
@KickeRockK

So download 1 time all records and then "share".
Write yourself: get the total number of entries - divide by the number of entries that should be on the page = number of pages.
Here (2016!!!) it is written that there is limitToFirst(N).
Knowing the records-per-page - we get those that should be displayed.

A
AntonPolyakin, 2020-06-27
@AntonPolyakin

I'll expand on the answer a bit. The Firebase Database REST API has a shallow=true query parameter that allows you to get data as a list of ids. This list looks like this:

{
-M9yIcQN7_DgcYVCBl6b: true,
-MAlDOMreZGUdg7quSjY: true,
-MAlDRLIsXnuF3zhgQyz: true,
-MABPqHqh7S0Gc4EKwLs: true,
...
}

This is a fairly compact data format. To make pagination, you need to sort this list in alphabetical order, and then "divide". This is the only possible option, but since the data is presented in a compact form, loading all the records, even if you have several thousand of them, will not be a big problem, especially if your project is a SPA application. In SPA, you can do without repeated GET requests to the address ( https://[PROJECT_ID].firebaseio/.json?shallow=true), you can just send the request 1 time (at initialization), save the server response and use the list of identifiers for page generation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question