2009-03-03 8 views
2

< < 1 2 3 4 ... 15 17 ... 47 48 49 50 >>Une mise à l'échelle Paginator

< 7 ... 47 48 49 50 >>

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

(le gras est la page sélectionnée)

Existe-t-il une logique de découpage qui crée une pagination de mise à l'échelle comme celle-ci? J'ai créé l'un d'entre eux auparavant, mais il a fini par être un gâchis de déclarations logiques.

La langue que je suis en train de faire est PHP, mais si vous avez des exemples ou des conseils pour n'importe quelle langue, ce serait apprécié.

Par mise à l'échelle, je veux dire quand il y a seulement quelques pages. La pagination l'affiche.

< 5 6 7 >>

Comme le nombre de pages croître jusqu'à un certain point, l'arrêt de pagination montrant tous les nombres et commence à les séparer.

< < 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 16 17 ... 47 48 49 50 >>

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

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

>>

(note, les chiffres réels et combien il montre avant et après n'est pas pertinent)

+0

Pourriez-vous expliquer plus ce que vous voulez dire par "mise à l'échelle"? –

Répondre

2

Désolé pour le blob de code, mais va ici. Espérons que les commentaires sont suffisants pour vous dire comment cela fonctionne - si laisser un commentaire et je pourrais ajouter un peu plus.

/** 
    * 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

Merci, je ne suis pas allé avec votre code exactement, mais cela m'a aidé à apprendre où je peux améliorer le mien. –