@model TONA.Repositories.GenericClasses.PagedResultBase
@{
int currentPage = Model.CurrentPage;
int countPages = Model.PageCount;
var generateUrl = Model.GenerateUrl;
int? preview = null;
int? next = null;
if (currentPage > countPages)
currentPage = countPages;
if (countPages <= 1) return;
if (currentPage > 1)
preview = currentPage - 1;
if (currentPage < countPages)
next = currentPage + 1;
List<int> pagesRanges = new List<int>();
int delta = 5;
int remain = delta * 2;
pagesRanges.Add(currentPage);
for (int i = 1; i <= delta; i++)
{
if (currentPage + i <= countPages)
{
pagesRanges.Add(currentPage + i);
remain--;
}
if (currentPage - i >= 1)
{
pagesRanges.Insert(0, currentPage - i);
remain--;
}
}
if (remain > 0)
{
if (pagesRanges[0] == 1)
{
for (int i = 1; i <= remain; i++)
{
if (pagesRanges.Last() + 1 <= countPages)
{
pagesRanges.Add(pagesRanges.Last() + 1);
}
}
}
else
{
for (int i = 1; i <= remain; i++)
{
if (pagesRanges.First() - 1 > 1)
{
pagesRanges.Insert(0, pagesRanges.First() - 1);
}
}
}
}
}
<div class="d-flex">
<ul class="pagination list-inline mx-auto justify-content-center">
<!-- Previous page link -->
@if (preview != null)
{
<li class="page-item">
<a class="page-link" href="@generateUrl(1)">«</a>
</li>
<li class="page-item">
<a class="page-link" href="@generateUrl(preview)">‹</a>
</li>
}
else
{
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">«</a>
</li>
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">‹</a>
</li>
}
<!-- Numbered page links -->
@foreach (var pageitem in pagesRanges)
{
if (pageitem != currentPage)
{
<li class="page-item">
<a class="page-link" href="@generateUrl(pageitem)">
@pageitem
</a>
</li>
}
else
{
<li class="page-item active" aria-current="page">
<a class="page-link" href="#">@pageitem <span class="sr-only">(current)</span></a>
</li>
}
}
<!-- Next page link -->
@if (next != null)
{
<li class="page-item">
<a class="page-link" href="@generateUrl(next)">›</a>
</li>
<li class="page-item">
<a class="page-link" href="@generateUrl(countPages)">»</a>
</li>
}
else
{
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">›</a>
</li>
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">»</a>
</li>
}
</ul>
</div>