Created
January 28, 2021 08:45
-
-
Save yigith/c6f999788b833dc3d22ac6332a053dd1 to your computer and use it in GitHub Desktop.
Pagination Numbers (C#)
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
// https://gist.github.com/kottenator/9d936eb3e4e3c3e02598 | |
public static int[] Pagination(int current, int last) | |
{ | |
int delta = 2; | |
int left = current - delta; | |
int right = current + delta + 1; | |
var range = new List<int>(); | |
var rangeWithDots = new List<int>(); | |
int? l = null; | |
for (var i = 1; i <= last; i++) | |
{ | |
if (i == 1 || i == last || i >= left && i < right) | |
{ | |
range.Add(i); | |
} | |
} | |
foreach (var i in range) | |
{ | |
if (l != null) | |
{ | |
if (i - l == 2) | |
{ | |
rangeWithDots.Add(l.Value + 1); | |
} | |
else if (i - l != 1) | |
{ | |
rangeWithDots.Add(-1); | |
} | |
} | |
rangeWithDots.Add(i); | |
l = i; | |
} | |
return rangeWithDots.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment