Algorithm / pseudo-code to create paging links?

后端 未结 6 1227
时光取名叫无心
时光取名叫无心 2020-12-07 23:46

Can someome provide code or pseudo-code for how the paging links on StackOverflow are generated?

I keep racking my brain but can\'t think of a decent way to build th

6条回答
  •  一向
    一向 (楼主)
    2020-12-08 00:08

    This is my approach to make a paging link. The following java code is just a pseudo.

    package com.edde;
    
    /**
     * @author Yang Shuai
     */
    public class Pager {
    
        /**
         * This is a method used to display the paging links(pagination or sometimes called pager).
         * The totalPages are the total page you need to display. You can get this value using the
         * formula:
         * 
         *     total_pages = total_records / items_per_page
         * 
         * This methods is just a pseudo-code.
         * 
         * 
         * @param totalPages how many pages you need to display
         * @param currentPage you are in which page now
         */
        public static void printPageLinks(int totalPages, int currentPage) {
    
            // how many pages to display before and after the current page
            int x = 2;
    
            // if we just have one page, show nothing
            if (totalPages == 1) {
                return;
            }
    
            // if we are not at the first page, show the "Prev" button
            if (currentPage > 1) {
                System.out.print("Prev");
            }
    
            // always display the first page
            if (currentPage == 1) {
                System.out.print("    [1]");
            } else {
                System.out.print("    1");
            }
    
            // besides the first and last page, how many pages do we need to display?
            int how_many_times = 2 * x + 1;
    
            // we use the left and right to restrict the range that we need to display
            int left = Math.max(2, currentPage - 2 * x - 1);
            int right = Math.min(totalPages - 1, currentPage + 2 * x + 1);
    
            // the upper range restricted by left and right are more loosely than we need,
            // so we further restrict this range we need to display
            while (right - left > 2 * x) {
                if (currentPage - left < right - currentPage) {
                    right--;
                    right = right < currentPage ? currentPage : right;
                } else {
                    left++;
                    left = left > currentPage ? currentPage : left;
                }
            }
    
            // do we need display the left "..."
            if (left >= 3) {
                System.out.print("    ...");
            }
    
            // now display the middle pages, we display how_many_times pages from page left
            for (int i = 1, out = left; i <= how_many_times; i++, out++) {
                // there are some pages we need not to display
                if (out > right) {
                    continue;
                }
    
                // display the actual page
                if (out == currentPage) {
                    System.out.print("    [" + out + "]");
                } else {
                    System.out.print("    " + out);
                }
            }
    
            // do we need the right "..."
            if (totalPages - right >= 2) {
                System.out.print("    ...");
            }
    
            // always display the last page
            if (currentPage == totalPages) {
                System.out.print("    [" + totalPages + "]");
            } else {
                System.out.print("    " + totalPages);
            }
    
            // if we are not at the last page, then display the "Next" button
            if (currentPage < totalPages) {
                System.out.print("    Next");
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            // printPageLinks(50, 3);
            help(500);
        }
    
        public static void test(int n) {
            for (int i = 1; i <= n; i++) {
                printPageLinks(n, i);
            }
            System.out.println("------------------------------");
        }
    
        public static void help(int n) {
            for (int i = 1; i <= n; i++) {
                test(i);
            }
        }
    
        public static void help(int from, int to) {
            for (int i = from; i <= to; i++) {
                test(i);
            }
        }
    
    }
    

提交回复
热议问题