From 63db4647ad0e048e134f24791dc098881aa02709 Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 15 May 2024 11:26:15 +0200 Subject: [PATCH] :bug: Fix label & method countResults to display good things with simple pager --- src/Datagrid/PagerInterface.php | 2 ++ src/Datagrid/SimplePager.php | 28 ++++++++++++++++--- src/Resources/views/CRUD/base_list.html.twig | 2 +- .../Pager/simple_pager_results.html.twig | 3 +- tests/App/Datagrid/Pager.php | 5 ++++ tests/Datagrid/PagerTest.php | 2 +- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Datagrid/PagerInterface.php b/src/Datagrid/PagerInterface.php index 4a9395dc5e..b7de84d825 100644 --- a/src/Datagrid/PagerInterface.php +++ b/src/Datagrid/PagerInterface.php @@ -81,6 +81,8 @@ public function getCurrentPageResults(): iterable; public function countResults(): int; + public function displayCountResults(bool $rendering = true): int|string; + /** * Returns an array of page numbers to use in pagination links. * diff --git a/src/Datagrid/SimplePager.php b/src/Datagrid/SimplePager.php index 582da46d39..f25af1d30d 100644 --- a/src/Datagrid/SimplePager.php +++ b/src/Datagrid/SimplePager.php @@ -15,6 +15,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Sonata\AdminBundle\Util\TraversableToCollection; +use Traversable; /** * @author Lukas Kahwe Smith @@ -44,10 +45,8 @@ final class SimplePager extends Pager * If set to 3 the pager will generate links to the next three pages * etc. */ - public function __construct( - int $maxPerPage = 10, - private int $threshold = 1 - ) { + public function __construct(int $maxPerPage = 10, private int $threshold = 1) + { parent::__construct($maxPerPage); } @@ -56,6 +55,27 @@ public function countResults(): int return ($this->getPage() - 1) * $this->getMaxPerPage() + ($this->thresholdCount ?? 0); } + public function displayCountResults(bool $rendering = true): int|string + { + $countResults = $this->countResults() > $this->getMaxPerPage() + ? $this->getMaxPerPage() + : $this->countResults(); + + if ($this->getPage() > 1) { + $countResults = $this->countResults(); + } + + if ($this->getLastPage() === $this->getPage()) { + $rendering = false; + } + + if (!$rendering) { + return $countResults; + } + + return sprintf('%s+', $countResults); + } + public function getCurrentPageResults(): iterable { if (null !== $this->results) { diff --git a/src/Resources/views/CRUD/base_list.html.twig b/src/Resources/views/CRUD/base_list.html.twig index 5692b296e2..8fad584e5b 100644 --- a/src/Resources/views/CRUD/base_list.html.twig +++ b/src/Resources/views/CRUD/base_list.html.twig @@ -186,7 +186,7 @@ file that was distributed with this source code.