2009-03-03 70 views
2

< < 1 2 3 4 ... 15 17 ... 47 48 49 50 >>縮放分頁程序

< ... 47 48 49 50 >>

< < 1 2 3 4 ... 44 46 47 48 49 50 >>

(粗體是選定的頁面)

是否有切割邏輯在那裏創建像這樣的縮放分頁?我之前已經創建了其中一個,但它最終成了一團亂七​​八糟的邏輯陳述。

我現在正在做這個的語言是PHP,但如果您有任何語言的示例或技巧,它將不勝感激。

縮放我的意思是當只有幾頁時。分頁顯示了這一點。

< 5 6 7 >>

作爲頁數增長到某一點時,分頁停止展示的所有數字,並開始分裂它們。

< < 2 3 4 ... 47 48 49 50 >>

< 5 6 ... 47 48 49 50 >>

< 7 8 ... 47 48 49 50 >>

< < 1 2 3 4。7 9 ... 47 48 49 50 >>

< < 1 2 3 4 .. 15 17 ... 47 48 49 50 >>

< < 1 2 3 4 ... 44 46 47 48 49 50 >>

< < 1 2 3 4 ... 47 48 49 >>

(注意,實際的數字和它顯示的前後數量無關)

+0

你能解釋一下「縮放」是什麼意思嗎? – 2009-03-03 15:40:08

回答

2

對不起,代碼如下。希望評論足以告訴你它是如何工作的 - 如果留下評論,我可能會添加更多。

/** 
    * 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); 
    } 
+0

謝謝,我沒有準確地使用你的代碼,但它幫助我瞭解了哪些地方可以改進我的一些。 – 2009-03-04 09:17:32