Last active
April 27, 2017 11:08
-
-
Save pedroadaodev/b483e594fea147a9fc04b50e1ce0aebf to your computer and use it in GitHub Desktop.
Simple pagination buttons - bootstrap template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
// Get current page | |
int page = 1; | |
var itemsPerPage = 10; | |
if (HttpContext.Current != null) | |
{ | |
int.TryParse(HttpContext.Current.Request.QueryString["page"], out page); | |
if (page == 0) | |
{ | |
page = 1; | |
} | |
} | |
int skip = (page - 1) * itemsPerPage; | |
var myList = Repository.GetMyData().Skip(skip).Take(itemsPerPage).ToList(); | |
var listTotalItems = Repository.GetMyData().Count(); | |
var pagination = new PaginationModel(); | |
pagination.CurrentPage = page; | |
pagination.ItemsPerPage = itemsPerPage; | |
pagination.ListTotalItems = listTotalItems; | |
<!-- LIST MY ITEMS using foreach and my var myList --> | |
@Html.Partial("~/Views/Partials/pagination.cshtml", pagination) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model Placard.EstorilOpen.WebApplication.Models.PaginationModel | |
@{ | |
if (Model.ListTotalItems < Model.ItemsPerPage) | |
{ | |
return; | |
} | |
var maxPages = 10; | |
var totalPages = Model.ListTotalItems / Model.ItemsPerPage; | |
var totalPagesMultItemsPerPage = totalPages * Model.ItemsPerPage; | |
if (totalPagesMultItemsPerPage < Model.ListTotalItems) | |
{ | |
totalPages++; | |
} | |
var startPage = 1; | |
if (Model.CurrentPage > maxPages + 3) | |
{ | |
startPage = Model.CurrentPage - maxPages; | |
} | |
var endPage = totalPages; | |
if (Model.CurrentPage < totalPages - maxPages) | |
{ | |
endPage = Model.CurrentPage + maxPages; | |
} | |
var queryString = HttpUtility.ParseQueryString(Request.QueryString.ToString()); | |
queryString.Remove("page"); | |
} | |
@if (totalPages > 1) | |
{ | |
<nav aria-label="Page navigation"> | |
<p id="paginglabel" class="">Paginação @Model.CurrentPage</p> | |
<ul class="pagination"> | |
@if (Model.CurrentPage > 1) | |
{ | |
<li> | |
<a href="?page=@(Model.CurrentPage - 1)&@(queryString.ToString())" aria-label="Previous"> | |
<span aria-hidden="true">«</span> | |
</a> | |
</li> | |
} | |
@if (startPage > maxPages) | |
{ | |
<li> | |
<a href="?page=1&@(queryString.ToString())"> | |
<span class="sr-only">Page</span>01 | |
</a> | |
</li> | |
<li> | |
<a href="#" class="pagination__link"> | |
... | |
</a> | |
</li> | |
} | |
@for (int i = startPage - 1; i < endPage; i++) | |
{ | |
if (Model.CurrentPage == i + 1) | |
{ | |
<li class="active"> | |
<a href="?page=@(i+1)&@(queryString.ToString())"> | |
<span class="sr-only">Page</span>@((i + 1).ToString("00")) | |
</a> | |
</li> | |
} | |
else | |
{ | |
<li> | |
<a href="?page=@(i+1)&@(queryString.ToString())"> | |
<span class="sr-only">Page</span>@((i + 1).ToString("00")) | |
</a> | |
</li> | |
} | |
} | |
@if (Model.CurrentPage < totalPages - maxPages) | |
{ | |
<li> | |
<span class="pagination__link"> | |
... | |
</span> | |
</li> | |
<li> | |
<a href="?page=@(totalPages)&@(queryString.ToString())"> | |
<span class="sr-only">Page</span>@(totalPages.ToString("00")) | |
</a> | |
</li> | |
} | |
@if (Model.CurrentPage < (Model.ListTotalItems / Model.ItemsPerPage) | |
|| ((Model.CurrentPage == endPage-1) && Model.ListTotalItems % Model.ItemsPerPage != 0)) | |
{ | |
<li> | |
<a href="?page=@(Model.CurrentPage + 1)&@(queryString.ToString())" aria-label="Next"> | |
<span aria-hidden="true">»</span> | |
</a> | |
</li> | |
} | |
</ul> | |
</nav> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PaginationModel | |
{ | |
public int ItemsPerPage { get; set; } | |
public int CurrentPage { get; set; } | |
public int ListTotalItems { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment