I have a code that displays pagination on the page:
This displays all the elements as is - from the first to the MaxPage:
@if (Model.IsFirst()) { <li class="page-item disabled"><a class="page-link" href="@Model.GetUrl(1)"><<</a></li> <li class="page-item disabled"><a class="page-link" href="@Model.GetUrl(Model.PageNumber - 1)"><</a></li> } else { <li class="page-item"><a class="page-link" href="@Model.GetUrl(1)"><<</a></li> <li class="page-item"><a class="page-link" href="@Model.GetUrl(Model.PageNumber - 1)"><</a></li> } @for (var i = 1; i <= Model.MaxPageCount; i++) { if (Model.IsCurrent(i)) { <li class="page-item active"> <a class="page-link disabled" href="@Model.GetUrl(i)">@i</a> </li> } else { <li class="page-item"> <a class="page-link" href="@Model.GetUrl(i)">@i</a> </li> } }
Problems begin when the number of pages becomes more than 10 (and quite often in pagination and on three hundred pages happens) - they fly away for the right border of the screen.
I sit think what condition you need to take to pagination was shown beautifully.
Let's try to formalize the condition. There is some IEnumerable<int>
from 1 to Model.MaxPageCount and there is some number PageNumber (current page number) that belongs to this sequence.
Not sure exactly, but let's say I need to get no more than five elements BEFORE the current page and no more than five elements AFTER the current page.
At the same time, I don’t know how to handle the situation nicely, that if there are not enough pages (for example, we are looking at page 3, then there will be pages 1 and 2), then afterwards you probably should take a little more - not five, but three more. But also not the fact that AFTER the necessary number of pages suffices.
The question is rather a beautiful and simple algorithm (how is it usually done in typical paginators?), It is possible that I will write a specific formula myself. If someone has started with a similar task and knows how to do this, offer an option. I looked at various XPagedList options on github, but I didn’t find any convenient options.
RecalcList
: ru.stackoverflow.com/a/616413/218063 - Andrey NOP