代码之家  ›  专栏  ›  技术社区  ›  Ólafur Waage

缩放分页器

  •  2
  • Ólafur Waage  · 技术社区  · 15 年前

    <<1 2 3 4 ... 15 17 ... 47 48 49 50>&燃气轮机;

    <<1 2 3 4 5 6. 7.47 48 49 50>&燃气轮机;

    <<1 2 3 4 ... 44 45

    (粗体为所选页面)

    有没有切割器逻辑可以创建这样的缩放分页?我以前创建过其中一个,但最终它变成了一堆逻辑语句。

    我现在使用的语言是PHP,但如果您有任何语言的示例或提示,将不胜感激。

    <<1 2 3 4. 5.6.7>&燃气轮机;

    随着页数增长到某一点,分页将停止显示所有数字,并开始将其拆分。

    << 1. 2 3 4 ... 47 48 49 50>&燃气轮机;

    << 4. 5 6 ... 47 48 49 50>&燃气轮机;

    << 6. 7 8 ... 47 48 49 50>&燃气轮机;

    << 9 ... 47 48 49 50>&燃气轮机;

    << 16

    <<1 2 3 4 ... 44 4647484950>&燃气轮机;

    <<1 2 3 4 ... 47 48 49 50 &燃气轮机&燃气轮机;

    (注意,实际数字及其前后显示的数量不相关)

    1 回复  |  直到 4 年前
        1
  •  2
  •   Greg    15 年前

    很抱歉,这是一堆代码。希望这些评论足以告诉你它是如何工作的-如果你留下评论,我可能会补充一些。

        /**
         * Get a spread of pages, for when there are too many to list in a single <select>
         * Adapted from phpMyAdmin common.lib.php PMA_pageselector function
         *
         * @param integer total number of items
         * @param integer the current page
         * @param integer the total number of pages
         * @param integer the number of pages below which all pages should be listed
         * @param integer the number of pages to show at the start
         * @param integer the number of pages to show at the end
         * @param integer how often to show pages, as a percentage
         * @param integer the number to show around the current page
         */
        protected function pages($rows, $pageNow = 1, $nbTotalPage = 1, $showAll = 200, $sliceStart = 5, $sliceEnd = 5, $percent = 20, $range = 10)
        {
            if ($nbTotalPage < $showAll)
                return range(1, $nbTotalPage);
    
            // Always show the first $sliceStart pages
            $pages = range(1, $sliceStart);
    
            // Always show last $sliceStart pages
            for ($i = $nbTotalPage - $sliceEnd; $i <= $nbTotalPage; $i++)
                $pages[] = $i;
    
            $i = $sliceStart;
            $x = $nbTotalPage - $sliceEnd;
            $met_boundary = false;
            while ($i <= $x)
            {
                if ($i >= ($pageNow - $range) && $i <= ($pageNow + $range))
                {
                    // If our pageselector comes near the current page, we use 1
                    // counter increments
                    $i++;
                    $met_boundary = true;
                }
                else
                {
                    // We add the percentate increment to our current page to
                    // hop to the next one in range
                    $i = $i + floor($nbTotalPage / $percent);
    
                    // Make sure that we do not cross our boundaries.
                    if ($i > ($pageNow - $range) && !$met_boundary)
                      $i = $pageNow - $range;
                }
    
                if ($i > 0 && $i <= $x)
                    $pages[] = $i;
            }
    
            // Since because of ellipsing of the current page some numbers may be double,
            // we unify our array:
            sort($pages);
            return array_unique($pages);
        }