silverstripe-framework: IsLast doesn’t work when PaginatedList has more than one page
Module version(s) affected
5.3.0
Description
In Silverstripe 4, <% if $First %> or <% if $Last %> worked as expected within the scope of the current page. I.e. they would check whether the current item in the iterator is the first/last item on that page.
In Silverstripe 5, $IsFirst still works this way (it returns true if the item is the first item on that page) but $IsLast doesn’t work at all in my testing. I would expect it to behave the same way as $Last did in Silverstripe 4. The bug only occurs when there is more than one page of items, otherwise it works as expected.
At this stage I’m not sure if it’s related to the switch from $First/$Last to $IsFirst/$IsLast, or related to the change to DataList to use generators. I suspect the latter, a good way to confirm would be to re-test with ArrayList.
How to reproduce
Set up a paginated list (e.g. in your Page class):
public function getPaginatedList()
{
$items = [
['Val' => 1],
['Val' => 2],
['Val' => 3],
['Val' => 4],
['Val' => 5],
['Val' => 6],
['Val' => 7],
['Val' => 8],
['Val' => 9],
['Val' => 10],
];
$list = new ArrayList($items);
$paginated = new PaginatedList($list);
$paginated->setPageLength(3);
$paginated->setCurrentPage(2);
return $paginated;
}
In a template, iterate through that paginated list:
$PaginatedList.TotalItems
<ul>
<% loop $PaginatedList %>
<li>$FirstLast $Val/$TotalItems</li>
<% end_loop %>
</ul>
The output in CMS 4:
10
first 4/3
5/3
last 6/3
The output in CMS 5:
10
first 4/10
5/10
6/10
Possible Solution
No response
Additional Context
If I add {$FromEnd}, it looks like SSViewer_BasicIteratorSupport is taking into account the total number of items in the un-paginated list, and not applying either the limit or offset:
Validations
- Check that there isn’t already an issue that reports the same bug
- Double check that your reproduction steps work in a fresh installation of
silverstripe/installer(with any code examples you’ve provided)
PRs
CMS 5
- https://github.com/silverstripe/silverstripe-framework/pull/11471
- https://github.com/silverstripe/silverstripe-framework/pull/11633
- https://github.com/silverstripe/developer-docs/pull/716
CMS 6
[!IMPORTANT] Merge the CMS 5 deprecation PRs first, then reassign to Guy so they can merge up and rebase the CMS 6 PRs on top.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (16 by maintainers)
Commits related to this issue
- FIX: Make IsFirst and IsLast work as expected for PaginatedList (fixes #11465) — committed to lozcalver/sapphire by lozcalver 2 years ago
- FIX: Make IsFirst and IsLast work as expected for PaginatedList (fixes #11465) — committed to lozcalver/sapphire by lozcalver 2 years ago
- FIX: Make IsFirst and IsLast work as expected for PaginatedList (fixes #11465) — committed to lozcalver/sapphire by lozcalver 2 years ago
- Merge pull request #11471 from kinglozzer/11465-paginatedlist-firstlast FIX: Make IsFirst and IsLast work as expected for PaginatedList (fixes #11465) — committed to silverstripe/silverstripe-framework by GuySartorelli 2 years ago
The original design of First/Last was for making alternative styling on when rendering the current view. So “last” meant the last item of the current page. I wouldn’t suggest changing this.