LostKobrakai

LostKobrakai

Numerator - calculate paginations without creating any markup

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

Most Liked

LostKobrakai

LostKobrakai

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.

LostKobrakai

LostKobrakai

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.

LostKobrakai

LostKobrakai

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:

Where Next?

Popular in Announcing Top

tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19483 141
New
pkrawat1
Presenting Aviacommerce, open source e-commerce platform in Elixir Aviacommerce is an open source e-commerce platform in Elixir. We at...
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamPipe ...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement