Pagination Buttons With Related Html Table
I recently was looking for a solution to generate and manage pagination buttons related to a large collection inside a table with Angular2. I would use it in a modal page such that the table doesn’t override it. I first try to attach a scroll bar to the tbody of the table to limit the size. This solution used css tricks and was not consistent according to the navigators.
Finally, I start implementing my own Angular2 component. This component could be attached to any collections. I start developing incrementally and I was quickly face to poor quality code and mutable state to determine the context. I stopped and meditate in a more functional way. I isolate the main functionality. This last can be formalized as:
- Given a collection and the current page
- Provide the list of Strings showing maximum 10 elements and comprising page number or ‘…’ if the number of pages is greater than 10. (Previous and Next buttons are not counted)
This illustrates the objective of the paginated buttons-group:
Considering I like functional programming even if I mainly use imperative paradigm, I try a first version in Haskell before reimplementing in JS.
Haskell Version:
Then, I think “great”. Let’s keep this version and look at how integrating it with a TypeScript-Angular2 project. I looked for some ways to compile Haskell to JS with ghcjs but for now, the project is not very robust. Other solutions: PureScript or ELM. I chose Purescript instead of ELM because this last focus on view.
I change the syntax a bit and arrive to this version:
Nice. It works perfectly with my Angular2 project but if I look at the generated JS compressed code: ~32000 characters and 113 Kio. It includes lots of PuresScript standard libraries.
Ok, I suggest using PureScript in projects including lots of PureScript code. But just for a couple of functionalities, it’s not wise. So I confess the defeat and write a TypeScript + Lodash version. Finally, this is not so bad:
You observed that I transform number to string with the map method. Js is weakly typed, so it is not mandatory but it remains coherent.
Check out the final Angular2 version on Plunker and the Gist repository for Haskell, PureScript and TypeScript version of the function paginate.