Skip to content

Instantly share code, notes, and snippets.

@yigith
Created January 28, 2021 08:45
Show Gist options
  • Save yigith/c6f999788b833dc3d22ac6332a053dd1 to your computer and use it in GitHub Desktop.
Save yigith/c6f999788b833dc3d22ac6332a053dd1 to your computer and use it in GitHub Desktop.
Pagination Numbers (C#)
// 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