Adaptive/dynamic page numbering in c#

If you need to show many results (tens, hundreds or thousands), in a paged manner, you won’t want to display links to all those pages. I looked online for some c# that would adapt the number of page links shown to with the number of pages returned, but found nothing.

If you get 5 pages of results, show them all. If you get 5,000 pages of results, show some around your current page and less and less the further you get from your current page.

This is something I put together in an hour, I make no claims about its efficiency and there are scenarios that are not catered for (like higher current page than the total number of pages).

Here are some examples of what this code does.

Current Page:1 Total Pages:40 Pages to display around current page:5 1 2 3 4 5 6 7 8 9 10 11 20 30 40

Current Page:90 Total Pages:600 Pages to display around current page:5 1 40 50 60 70 80 85 86 87 88 89 90 91 92 93 94 95 100 110 120 130 140 200 300 400 500 600

Current Page:147 Total Pages:6825 Pages to display around current page:5 1 90 100 110 120 130 140 142 143 144 145 146 147 148 149 150 151 152 160 170 180 190 200 300 400 500 600 700 800 900 1000 1100 2000 3000 4000 5000 6000 6825

You could easily change the code to take a Page type to suit your own needs.

Here’s the code.

 1namespace NoDogmaBlog
 2{
 3    public class PagingHelper
 4    {
 5        public IEnumerable<int> GetListOfPages(int currentPage, int pagesAroundCurrent, int totalPages)
 6        {
 7            var pages = new Dictionary<int, int>();
 8            double powerOfTenTotalPages = Math.Floor(Math.Log10(totalPages));
 9            if ((int)powerOfTenTotalPages == 0)
10            {
11                powerOfTenTotalPages = 1;
12            }
13            pages.Add(1, 1);
14            if (!pages.ContainsKey(totalPages))
15            {
16                pages.Add(totalPages, totalPages);
17            }
18
19            for (int loop = 1; loop <= powerOfTenTotalPages + 1; loop++)
20            {
21                GetPages(pages, currentPage, pagesAroundCurrent, totalPages, (int)Math.Pow(10, loop - 1));
22            }
23            return pages.OrderBy(k=>k.Key).Select(p=>p.Key).AsEnumerable();
24        }
25
26        private void GetPages(Dictionary<int, int> pages, int currentPage, int pagesAroundCurrent, int totalPages, int jump)
27        {
28            int startPage = ((currentPage / jump) *  jump) - (pagesAroundCurrent *  jump);
29
30            if (startPage < 0)
31            {
32                startPage = 0;
33                pagesAroundCurrent = 10;
34            }
35
36            int endPage = currentPage + (pagesAroundCurrent *  jump);
37            if (endPage > totalPages)
38            {
39                endPage = totalPages;
40            }
41            AddPagesToDict(pages, startPage, endPage, jump);
42        }
43
44        private void AddPagesToDict(Dictionary<int, int> pages, int start, int end, int jump)
45        {
46            for (int loop = start; loop <= end; loop += jump)
47            {
48                if (!pages.ContainsKey(loop))
49                {
50                    if (loop > 0)
51                    {
52                        pages.Add(loop, loop);
53                    }
54                }
55            }
56        }
57    }
58}
comments powered by Disqus

Related