Skip to content

Instantly share code, notes, and snippets.

@Glutexo
Last active April 19, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glutexo/54efb5601b837f533953a5b2c390bfd3 to your computer and use it in GitHub Desktop.
Save Glutexo/54efb5601b837f533953a5b2c390bfd3 to your computer and use it in GitHub Desktop.
What is the correct way to build the next and previous links in limit/offset pagination?

10 records

Aligned to begin

?limit=3&offset=0

----------
111        ?limit=3&offset=0
   222     ?limit=3&offset=3
      333  ?limit=3&offset=6
         4 ?limit=3&offset=9

Aligned to end

?limit=3&offset=-2

----------
1          ?limit=3&offset=-2
 222       ?limit=3&offset=1
    333    ?limit=3&offset=4
       444 ?limit=3&offset=7

Arbitrary

?limit=3&offset=-1

----------
11         ?limit=3&offset=-1
  222      ?limit=3&offset=2
     333   ?limit=3&offset=5
        44 ?limit=3&offset=8
GET ?limit=5

      ----------
First AAAAA      "?limit=5&offset=0"
Last     AAAAA   "?limit=5&offset=5"
GET ?limit=5&offset=2

	    ----------
Current       AAAAA
Previous a)            null
Previous b) BB         "?limit=2&offset=0"
Previous b) CCCCC      "?limit=5&offset=0"
GET ?limit=5&offset=4

        ----------
Current     AAAAA
Next a)            null
Next b)          B "?limit=1&offset=9"
Next c)          C "?limit=5&offset=9"
Next d)      DDDDD "?limit=5&offset=5"
GET ?limit=5&offset=8

        ----------
Current         AA
Next a)            null
Next b)          B "?limit=1&offset=9"
Next c)          C "?limit=5&offset=9"
GET ?limit=5&offset=3

         ----------
Current     AAAAA
Previous AAA        "?limit=5&offset=-2"
Next             AA "?limit=5&offset=8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment