Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix label & method countResults to display good things with simple pager #8183

Draft
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Datagrid/PagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
28 changes: 24 additions & 4 deletions src/Datagrid/SimplePager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Sonata\AdminBundle\Util\TraversableToCollection;
use Traversable;

/**
* @author Lukas Kahwe Smith <[email protected]>
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/base_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ file that was distributed with this source code.
<label class="checkbox" for="{{ admin.uniqid }}_all_elements">
<input type="checkbox" name="all_elements" id="{{ admin.uniqid }}_all_elements">
{{ 'all_elements'|trans({}, 'SonataAdminBundle') }}
({{ admin.datagrid.pager.countResults() }})
({{ admin.datagrid.pager.displayCountResults() }})
</label>

<select name="action" style="width: auto; height: auto" class="form-control">
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/views/Pager/simple_pager_results.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ file that was distributed with this source code.
{% if admin.datagrid.pager.lastPage != admin.datagrid.pager.page %}
{{ 'list_results_count_prefix'|trans({}, 'SonataAdminBundle') }}
{% endif %}
{% trans with {'%count%': admin.datagrid.pager.countResults()} from 'SonataAdminBundle' %}list_results_count{% endtrans %}
{% set rendering_count_results = false %}
{% trans with {'%count%': admin.datagrid.pager.displayCountResults(rendering_count_results)} from 'SonataAdminBundle' %}list_results_count{% endtrans %}
&nbsp;-&nbsp;
{% endblock %}

Expand Down
5 changes: 5 additions & 0 deletions tests/App/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public function countResults(): int
return 1;
}

public function displayCountResults(bool $rendering = true): int|string
{
return 1;
}

public function getLinks(?int $nbLinks = null): array
{
return [];
Expand Down
2 changes: 1 addition & 1 deletion tests/Datagrid/PagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function setUp(): void
true,
true,
true,
['countResults']
['countResults', 'displayCountResults']
);
}

Expand Down