LostKobrakai

LostKobrakai

For pagination many people (like me) use scrivener and scrivener_ecto, which works great for offset based pagination. What never really fit my work though was scrivener_html, because I often don’t use frameworks and it took over link generation and styles so extensively.

A thread here on the forums today made me clean up some code I wrote a few days ago for a new project and release it on hex:

https://github.com/madeitGmbH/numerator

The package does not generate any markup at all, but instead creates a list of elements, which describe the pagination. You need to keep GET parameters on the urls, because the paginated content is filtered? Numerator doesn’t care. You want to use custom styles? Go ahead. You need to display the pagination in a CLI? No problem.

iex(1)> Numerator.build(%{page: 6, last: 17}, show_first: true, show_last: true)
[
  %{page: 5, type: :prev},
  %{page: 1, type: :page},
  %{type: :ellipsis},
  %{page: 4, type: :page},
  %{page: 5, type: :page},
  %{page: 6, type: :current},
  %{page: 7, type: :page},
  %{page: 8, type: :page},
  %{type: :ellipsis},
  %{page: 17, type: :page},
  %{page: 7, type: :next}
]

It does support a scrivener page struct as first argument as well, so there’s no need to convert the map parameters manually.

I’ve also added an example implementation for bootstrap in the readme: numerator/README.md at master · LostKobrakai/numerator · GitHub

Showing Posts 1 to 6

LostKobrakai

LostKobrakai OP

I just released an update to the package:

0.2.1

  • Extracted the chunk generation; The part calculating which pages around the current one to show.
  • Improved the performance especially when paginating a large number of pages

Small number: 1..100; Large number: 1..100_000; Number of pages to show: 5

Before:

Name                            ips        average  deviation         median         99th %
Small number of pages       23.33 K      0.0429 ms     ±7.68%      0.0420 ms      0.0520 ms
Large number of pages      0.0169 K       59.31 ms     ±9.67%       59.26 ms       73.17 ms

Comparison:
Small number of pages       23.33 K
Large number of pages      0.0169 K - 1383.32x slower +59.26 ms

After:

Name                            ips        average  deviation         median         99th %
Small number of pages      219.65 K        4.55 μs   ±290.11%        3.99 μs        5.99 μs
Large number of pages      210.09 K        4.76 μs   ±298.51%        3.99 μs        5.99 μs

Comparison:
Small number of pages      219.65 K
Large number of pages      210.09 K - 1.05x slower +0.21 μs

Additionally I’d like to note, that for all of you using liveview this library might be worth looking into. It will allow proper change tracking as all the markup is handled in the template and not in some function call the template engine has no way to look into.

lucaong

lucaong

Nice library! I was looking for something similar and was about to build something myself. Your update message comes at the perfect time, as I missed your first announcement. Great job!

cnck1387

cnck1387

I recently started using this library. It works really nicely with scrivener_ecto.

LostKobrakai

LostKobrakai OP

I just pushed an update with a breaking change (0.3.0) where :show_first and :show_last pages are tagged :first and :last respectively – instead of just :page – when they’re separated from the normal pagination pages by an :ellipsis. So now they can be styled differently once the ellipsis starts appearing.

Kurisu

Kurisu

Please @LostKobrakai, is this still what you use or would recommend with the Numerator package today for pagination?

LostKobrakai

LostKobrakai OP

I haven’t been in the need for implementing pagination in a while, but I’d still use this. The returned list should play well with liveviews change tracking :smiley:

— All posts loaded —

Where Next? Top

Trending in Announcing Top

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
kip
I’ll shortly be launching Text, a nascent text analysis library. Current functionality In this early version (not ready for prime time) ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews