Skip to content

Instantly share code, notes, and snippets.

@nessthehero
Last active August 29, 2015 14:07
Show Gist options
  • Save nessthehero/9b2b15e2ee1d00bff07d to your computer and use it in GitHub Desktop.
Save nessthehero/9b2b15e2ee1d00bff07d to your computer and use it in GitHub Desktop.
Paging explained

Paging explained

An example of logic to print paging numbers. You could take this further by adding ellipsis on either side or showing pages 1-5 and n-last at all times. But that's a bit too advanced for what this is demonstrating.

// Don't show previous if we're on page 1
@if (search.page > 1) {
<a href="?keyword=@keyword&amp;page=@(search.page-1)" class="prev">prev</a>
}
// Start with 5 less than current page, increment to 5 more than current page
@for (var i = (search.page - 5); i <= (search.page + 5); i += 1)
{
// Only print valid page numbers within range
if (i >= 1 && i <= search.totalpages) {
// If we're on the current page
if (i == search.page) {
// Print a static page number
<span>@i</span>
// If we're within 4 pages of the current page on either side...
} else if ((search.page - i) >= 4 || (search.page - i) < 4) {
// Print the page linked
<a href="@Url.ProcessUrl(Model.URL)?keyword=@keyword&amp;page=@(i)" class="pager">@i</a>
}
}
}
// Don't show next if we're on the last page
@if (search.page != search.totalpages) {
<a href="@Url.ProcessUrl(Model.URL)?keyword=@keyword&amp;page=@(search.page+1)" class="next">next</a>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment