Building Table Views with Phoenix LiveView by Peter Ullrich
Author:Peter Ullrich
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
A Small Deep Dive into How Pagination Works
We implement the pagination by adding the limit/2 and offset/2 statements to our query. The limit/2 call instructs the database to return only the number of entries we specify. The offset/2 statement instructs the database from where it should count the number of entries to return. As an example, if we set the offset to 10 and the limit to 20, the database will return 20 entries counting from the eleventh row, effectively ignoring the first 10 entries.
This is how we achieve the pagination. We calculate the offset by multiplying the current page number by the page size. In the UI, we present the page numbers as starting from 1, but in our database, the first page is page 0. Thatâs why we subtract 1 from the page number before calculating the offset. Letâs look at an example:
Letâs say we want to fetch the data for the first page with a page size of 10. We first subtract 1 from the page number, receiving a new page number of 0. Then we multiply the 0 with our page size of 10, and we get an offset of 0. This means that the database will return 10 entries beginning with the very first row, which is exactly what we want.
Now, letâs say we want the data for the second page. We subtract 1 from the page number of 2, resulting in a new page number of 1. We multiply it with our page size of 10 and get an offset of 10. This means that the database will skip the first 10 entries and return entries from the eleventh row onward. So, for our second page, we return the second chunk of data, which is exactly what we want as well.
As a safety precaution, we use the max/2 function to ensure that our page number is always equal to or greater than 0. In case we make a mistake somewhere and set the page number to 0 or even -1, this will fix the mistake by replacing it with 0.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ajax | Assembly Language Programming |
Borland Delphi | C & C++ |
C# | CSS |
Compiler Design | Compilers |
DHTML | Debugging |
Delphi | Fortran |
Java | Lisp |
Perl | Prolog |
Python | RPG |
Ruby | Swift |
Visual Basic | XHTML |
XML | XSL |
Deep Learning with Python by François Chollet(12566)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9336)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7038)
Microservices with Go by Alexander Shuiskov(6802)
Practical Design Patterns for Java Developers by Miroslav Wengner(6716)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6654)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6063)
The Art of Crafting User Stories by The Art of Crafting User Stories(5594)
NetSuite for Consultants - Second Edition by Peter Ries(5529)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5335)
Kotlin in Action by Dmitry Jemerov(5062)
