Skip to content

Instantly share code, notes, and snippets.

@RoLYroLLs
Created December 8, 2020 14:33
Show Gist options
  • Save RoLYroLLs/c165202f72a256938da15c916b1362b8 to your computer and use it in GitHub Desktop.
Save RoLYroLLs/c165202f72a256938da15c916b1362b8 to your computer and use it in GitHub Desktop.
public IEnumerable<object> Pages(int current, int pageCount) {
List<object> pages = new List<object>();
var delta = 7;
if (pageCount > 7) {
delta = current > 4 && current < pageCount - 3 ? 2 : 4;
}
var startIndex = (int)Math.Round(current - delta / (double)2);
var endIndex = (int)Math.Round(current + delta / (double)2);
if (startIndex - 1 == 1 || endIndex + 1 == pageCount) {
startIndex += 1;
endIndex += 1;
}
var to = Math.Min(pageCount, delta + 1);
for (int i = 1; i <= to; i++) {
pages.Add(i);
}
if (current > delta) {
pages.Clear();
var from = Math.Min(startIndex, pageCount - delta);
to = Math.Min(endIndex, pageCount);
for (int i = from; i <= to; i++) {
pages.Add(i);
}
}
if (pages[0].ToString() != "1") {
if (pages.Count() + 1 != pageCount) {
pages.Insert(0, "...");
}
pages.Insert(0, 1);
}
if ((int)pages.Last() < pageCount) {
if (pages.Count() + 1 != pageCount) {
pages.Add("...");
}
pages.Add(pageCount);
}
return pages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment