Parcourir la source

add ability set maximum number of page numbers = MaxPerPage or set from template

butt il y a 13 ans
Parent
commit
7e5714eb68
3 fichiers modifiés avec 41 ajouts et 2 suppressions
  1. 17 0
      Admin/Admin.php
  2. 23 1
      Datagrid/Pager.php
  3. 1 1
      Resources/views/CRUD/base_list.html.twig

+ 17 - 0
Admin/Admin.php

@@ -108,6 +108,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      */
     protected $maxPerPage = 25;
 
+    /**
+     * The maximum number of page numbers to display in the list
+     *
+     * @var integer
+     */
+    protected $maxPageLinks = 25;
+
     /**
      * The base route name used to generate the routing information
      *
@@ -650,6 +657,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $this->getFilterParameters());
 
         $this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
+        $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);
 
         $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
 
@@ -1290,6 +1298,15 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     {
         return $this->maxPerPage;
     }
+    public function setMaxPageLinks($maxPageLinks)
+    {
+        $this->maxPageLinks = $maxPageLinks;
+    }
+
+    public function getMaxPageLinks()
+    {
+        return $this->maxPageLinks;
+    }
 
     public function getFormGroups()
     {

+ 23 - 1
Datagrid/Pager.php

@@ -28,6 +28,7 @@ abstract class Pager implements \Iterator, \Countable, \Serializable, PagerInter
     protected $parameters = array();
     protected $currentMaxLink = 1;
     protected $maxRecordLimit = false;
+    protected $maxPageLinks = 0;
 
     // used by iterator interface
     protected $results = null;
@@ -90,8 +91,11 @@ abstract class Pager implements \Iterator, \Countable, \Serializable, PagerInter
      *
      * @return array
      */
-    public function getLinks($nb_links = 5)
+    public function getLinks($nb_links=null)
     {
+        if($nb_links==null){
+            $nb_links=$this->getMaxPageLinks();
+        }
         $links = array();
         $tmp = $this->page - floor($nb_links / 2);
         $check = $this->lastPage - $nb_links + 1;
@@ -364,6 +368,24 @@ abstract class Pager implements \Iterator, \Countable, \Serializable, PagerInter
             }
         }
     }
+    /**
+     * Returns the maximum number of page numbers.
+     *
+     * @return integer
+     */
+    public function getMaxPageLinks()
+    {
+        return $this->maxPageLinks;
+    }
+    /**
+     * Sets the maximum number of page numbers.
+     *
+     * @param integer $maxPageLinks
+     */
+    public function setMaxPageLinks($maxPageLinks)
+    {
+        $this->maxPageLinks = $maxPageLinks;
+    }
 
     /**
      * Returns true if on the first page.

+ 1 - 1
Resources/views/CRUD/base_list.html.twig

@@ -88,7 +88,7 @@ file that was distributed with this source code.
                                       {% endif %}
 
                                       {# Set the number of pages to display in the pager #}
-                                      {% for page in admin.datagrid.pager.getLinks(5) %}
+                                      {% for page in admin.datagrid.pager.getLinks() %}
                                           {% if page == admin.datagrid.pager.page %}
                                               <li class="active"><a href="{{ admin.generateUrl('list', admin.modelmanager.paginationparameters(admin.datagrid, page)) }}">{{ page }}</a></li>
                                           {% else %}