shahryarjb

shahryarjb

How to create pagination for a list (get elements from a range of GenServer)

Hi, I have a big list in my GenServer which I want to load them as a pagination, for example I counted it and I have x records and I want to load it with a page size (like 20).

my user inputs: page 1 and this pagination should load (1 - 20) records and if my user inputs page 2 , it should loads (21 - 40).

I saw Stream.resource but I couldn’t use it

Please help me to understand this!!

Thanks

Most Liked

tomkonidas

tomkonidas

Not sure if this would be the best way

Stream.chunk_every(list_of_items, 20)
|> Enum.at(page - 1)
RudManusachi

RudManusachi

In BEAM lists are “linked” lists… so any page > 1 would require somehow to traverse the list… Either if you skip first N and take M elements… or if you “chunk_every” M and take Nth chunk of it.

We need some sort an indexing… What about storing in a map where key is the index? Then based on the request you quickly figure out the required keys and do Map.take/2?

Or maybe store somewhere that supports pagination? :ets and :mnesia support match specs… But to rephrase a regex joke “if you decide to use a match spec you now have two problems” :grinning_face_with_smiling_eyes:

Depends on the task =)

trisolaran

trisolaran

If your elements are in memory, then you can just do:

page_size = 10
page_num = 3

list_of_elements
|> Enum.drop(page_size * (page_num - 1)) 
|> Enum.take(page_size)

This will give you the elements on the 3rd page (21 through 30).

However, keeping a large list in memory and implementing pagination in this way is potentially inefficient.
May ask you where your data is loaded from? It may be more efficient to offload pagination to the data source. For example, if the data source is an SQL database, you could fetch the elements for a given page with:

SELECT * FROM DATA
ORDER BY <SOMETHING>
OFFSET page_size * (page_num - 1)
LIMIT page_size

In this way only the elements of the desired page will be loaded every time, and with the proper indexes set up in your DB the query will be fast.

karlosmid

karlosmid

Hi! This is a very useful library:

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement