Created
December 8, 2020 14:33
-
-
Save RoLYroLLs/c165202f72a256938da15c916b1362b8 to your computer and use it in GitHub Desktop.
C# Pagination inspired by @jorrit91 version (https://gist.github.com/kottenator/9d936eb3e4e3c3e02598#gistcomment-3238804) of simple-pagination.js (https://gist.github.com/kottenator/9d936eb3e4e3c3e02598)
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 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